Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

task 调度执行存在并发问题 #2

Open
vbirds opened this issue Oct 26, 2022 · 0 comments
Open

task 调度执行存在并发问题 #2

vbirds opened this issue Oct 26, 2022 · 0 comments

Comments

@vbirds
Copy link

vbirds commented Oct 26, 2022

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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant