libgo icon indicating copy to clipboard operation
libgo copied to clipboard

可以动态添加协程吗

Open dreamyphone opened this issue 4 years ago • 2 comments

如果事先并不知道有多少个协程,运行过程中可以动态添加协程吗? 在sample2_yield.cpp 中尝试把co_sched.Start() 方法移动到最前面,结果就运行不起来了。 请问这部分应该怎么操作呢?

dreamyphone avatar Apr 12 '20 12:04 dreamyphone

co_sched.goStart()

yyzybb537 avatar May 06 '20 09:05 yyzybb537

看一下void Scheduler::Start的代码最后一行mainProc->Process(); 这个方法是死循环去处理提交给它的协程。所以Start方法不会退出。

要想继续添加协程,只能将co_sched.Start() 放在其他线程中运行。 比如sample1中的std::thread t([]{ co_sched.Start(); });

作者也封装成了co_sched.goStart()

sunny-shu avatar Aug 27 '20 08:08 sunny-shu