Giter VIP home page Giter VIP logo

gocoroutine's People

Contributors

davyxu avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

gocoroutine's Issues

task 调度执行存在并发问题

benchmark_test.go

package gocoroutine

import (
	//	"fmt"
	"sync"
	"testing"
)

var k int = 0
var m int = 0

func msgProc_benchmark(fc FlowControl) {

	//fmt.Println("msg 1")
	fc.Yield(dbProc_benchmark)

}

func dbProc_benchmark(fc FlowControl) {

	//fmt.Println("db 1")
	//	msgid := fc.Params().(int)
	k++
}

func recvProc_benchmark(sch *Scheduler) {

	for i := 0; i < 100000; i++ {
		sch.AddTask(msgProc_benchmark, i)
	}

}

func TestBenchmark(t *testing.T) {

	sch := NewScheduler()

	sch.Start()

	recvProc_benchmark(sch)

	sch.Exit()
	println("final k:\n", k)

}

func traditionalLogic(wg *sync.WaitGroup) {

	wg.Add(1)
	m++
	go func() {

		wg.Done()
	}()
}

// go test -bench=Benchmark -cpuprofile=cprof
func TestTraditional(t *testing.T) {

	var wg sync.WaitGroup

	for i := 0; i < 100000; i++ {

		traditionalLogic(&wg)
	}

	wg.Wait()
	println("final m:\n", m)

}

Scheduler 并不是单线程执行存在并发问题,执行结果为 99109 与 10000不符合

=== RUN   TestBenchmark
final k:
 99096
--- PASS: TestBenchmark (0.17s)
=== RUN   TestTraditional
final m:
 100000
--- PASS: TestTraditional (0.02s)
PASS

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.