普通Thread的调用方式,要传递一个方法进去,进行调用,查了下win和linux是如此设计,所以thread也这样。这点与java不同
void pause_thread(int n) {
this_thread::sleep_for(chrono::seconds(n));
cout << "pause of" << n << "seconds ended";
thread t(hello);
cout << "id:"<<t.hardware_concurrency << endl;
cout << "native_handle" << t.native_handle()<<endl;
t.join();
thread a(hello);
a.detach();
thread threads[5];
cout << "Spawning 5 threads...\n";
for (int i = 0; i < 5; i++)
{
threads[i] = thread(pause_thread, i + 1);
}
cout << "Done spawning threads.Now waiting for them to join:\n";
for (auto& thread : threads) {
thread.join();
}
cout << "All threads joined!\n";
本文探讨了如何在Windows和Linux平台中使用普通Thread传递方法并暂停,与Java的差异,并通过代码实例展示了线程创建、暂停和同步操作。重点在于thread对象的使用和线程间协作。
1342

被折叠的 条评论
为什么被折叠?



