问题:如何在后台设置计时器?那就是创建计时器线程的线程在时钟滴答作响时仍然可以做其他事情。
尝试:-使用 _beginthreadex() --> 似乎有竞争条件
class Timer{
...
static unsigned __stdcall tick(void *param){
while(1){
Timer::timer++;
Sleep(Timer::timer*1000);
}
return 1;
}
}
.....
HANDLE time_thread = (HANDLE) _beginthreadex(0, 0, &Timer::tick,0,0,NULL);
...
//test for 20 seconds
//want to do something while the clock is not 20 seconds
//the mainthread here still has to receive input
//What is the proper way to do it?
while (Timer::getTime() != 20){
cout << Timer::getTime()
}
CloseHandle(time_thread);
...
注意:我使用的是 Visual Studio 2008,而不是 11,所以我没有 C++11 支持。