【发布时间】:2020-11-23 05:44:26
【问题描述】:
我需要您的帮助来解决我们的桌面应用程序面临的随机挂起问题。
我们在单例类中实现了一个线程,以循环调用 Beep 和 Sleep API。所以我们启动线程来调用这些,然后在一段时间后,我们打破循环,在主线程中等待单个对象并退出线程。就在从线程函数返回之前,我们调用_endthreadex。这里有一个挂起。现在我不知道 _endthreadex 是否失败以及它如何在超时后停止 waitforsingleobject 发出信号。该代码的实现与 _beginthreadex 的示例非常相似。正如我所说,它非常随机地发生在少数机器上。
unsigned __stdcall SecondThreadFunc( void* pArguments )
{
printf( "In second thread...\n" );
while( m_iBeepMode == iBEEPON )
{
Beep(600, 100);
Sleep(100);
}
//I added a logging here, which gets logged in hang case.
_endthreadex( 0 );
return 0;
}
int main()
{
HANDLE hThread;
unsigned threadID;
printf( "Creating second thread...\n" );
// Create the second thread.
hThread = (HANDLE)_beginthreadex( NULL, 16384, &SecondThreadFunc, this, 0, &threadID );
// the below code is not exactly in the same method. We call it at a later time to stop the beep.
m_iBeepMode = BEEPOFF;
WaitForSingleObject( hThread, 10000 );
// Added a logging here, which doesn't get logged.
// Destroy the thread object.
CloseHandle( hThread );
}
【问题讨论】:
标签: c++ multithreading windows-7