#include <thread>
#include <chrono>
using namespace std:
void f()
{
// Sleeping for a very long while
while (SOCKET s = accept(listening_socket, ...))
{
// ...
}
}
int main()
{
std::thread t(f);
DoSomething();
t.???(); /* What to place here to wake/terminate thread f? */
}
在Win32下,我可以使用TerminateThread()
杀死一个线程。但我想要的是一个跨平台的方法来做到这一点。如何正常唤醒或终止睡眠std ::线程?
我应该如何在C++中优雅地做到这一点?
设计多线程应用程序的一个原则是:“永不杀死线程,让他们自杀。” – 2013-02-15 01:33:21
为什么你想让它从第一步开始睡很久? – billz 2013-02-15 01:38:12
对'main'有一个引用而不是一个睡眠的条件变量进行定时等待,并且如果条件触发然后返回。 – ildjarn 2013-02-15 01:39:08