#include<iostream> #include<thread> #include<vector> #include<algorithm> int main() { std::vector<std::thread> threadVec; for(int i=0; i<5; ++i){ threadVec.push_back(std::thread([]() { std::cout<<"thread function\n"; })); } std::cout<<"main thread\n"; std::for_each(threadVec.begin(), threadVec.end(), [](std::thread & thr) { thr.join(); }); return 0; }运行结果为:
for(int i=0; i<5; ++i){ threadVec.push_back(std::thread([i]() { std::cout<<"thread function "<<i <<"\n"; })); } std::cout<<"main thread\n";得到的结果是:
C++11线程指南(二)--Lambda线程实现,布布扣,bubuko.com
原文:http://blog.csdn.net/shltsh/article/details/38393529