yyzybb537 / libgo

Go-style concurrency in C++11
MIT License
3.18k stars 754 forks source link

协程内外都调用的同一个函数,函数内有锁有休眠,必锁 #298

Closed gooker closed 1 year ago

gooker commented 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; }