阅读背景:

为什么这些线程不按顺序运行呢?

来源:互联网 

When I run this code:

当我运行这个代码:

#include <iostream>
#include <thread>
#include <mutex>

std::mutex m;

int main()
{
    std::vector<std::thread> workers;
    for (int i = 0; i < 10; ++i)
    {
        workers.emplace_back([i]
        {
            std::lock_guard<std::mutex> lock(m);
            std::cout << "Hi from thread " << i << std::endl;
        });
    }

    std::for_each(workers.begin(), workers.end(), [] (std::thread& t)
    { t.join(); });
}
#include <i



你的当前访问异常,请进行认证后继续阅读剩余内容。

分享到: