Closed gooker closed 1 year ago
测试是循环创建协程,协程内执行一个函数,有锁, 并且协程外的循环也执行函数加锁, 如果libgo协程就是这样,只能用libgo自带的锁吗 下面例子格式不一定对
void main() { co::Scheduler* sched = co::Scheduler::Create(); sched->goStart();
while (true) { co_sleep(200); for(int i = 0;i<10;i++) { go co_scheduler(sched) [&]{ test_mutex(); }; } test_mutex(); test_mutex(); } sched->Stop();
}
void test_mutex() { static std::mutex m; std::lock_guard lock(m); co_sleep(300); std::cout<<"test_mutex"<<std::endl; }
测试是循环创建协程,协程内执行一个函数,有锁, 并且协程外的循环也执行函数加锁, 如果libgo协程就是这样,只能用libgo自带的锁吗 下面例子格式不一定对
void main() { co::Scheduler* sched = co::Scheduler::Create(); sched->goStart();
}
void test_mutex() { static std::mutex m; std::lock_guard lock(m);
co_sleep(300);
std::cout<<"test_mutex"<<std::endl;
}