prettify

Oct 17, 2016

C++11: lambda, bind, thread

#include <iostream>
#include <thread>
#include <vector>
#include <algorithm>
int main()
{
    // vector container stores threads
    std::vector<std::thread> workers;
    for (int i = 0; i < 5; i++) {
        workers.push_back(std::thread([i]() 
        {
            std::cout << i << std::endl;
        }));
    }

    std::for_each(workers.begin(), workers.end(), [](std::thread &t) 
    {
        t.join();
    });
    std::cout << "end\n";

    return 0;
}
11:21am - hi ran 35 lines of C++ (finished in 1.29s):
14 
0 
2 
 
3 
end 

No comments:

Post a Comment