我想我犯了一个简单的错误,但由于我注意到这里有很多 boost 专家,所以我想我会寻求帮助。
我正在尝试在 Windows XP 上使用 boost threads(1_40)。主程序加载一个dll,像这样启动线程(注意这不是在一个类中,静态并不意味着对类静态而是对文件私有(private))。
static boost::thread network_thread;
static bool quit = false;
HANDLE quitEvent;
//some code omitted for clarity, ask if you think it would help
void network_start()
{
HANDLE *waitHandles = (HANDLE*)malloc(3 * sizeof(HANDLE));
waitHandles[0] = quitEvent;
waitHandles[1] = recvEvent;
waitHandles[2] = pendingEvent;
do {
//read network stuff, or quit event
dwEvents =WaitForMultipleObjects(3, waitHandles, FALSE, timeout);
} while (!quit)
}
DllClass::InitInstance()
{
}
DllClass::ExportedFunction()
{
network_thread = boost::thread(boost::bind<void>(network_start));
}
DllClass::ExitInstance()
{
//signal quit (which works)
quit = true;
SetEvent(QuitEvent);
//the following code is slightly verbose because I'm trying to figure out what's wrong
try {
if (network_thread.joinable() ) {
network_thread.join();
} else {
TRACE("Too late!");
}
} catch (boost::thread_interrupted&) {
TRACE("NET INTERRUPTED");
}
}
问题是主线程卡在join上,网络线程卡在_endthreadex的最后。我误会了什么?
最佳答案
您不应该在 InitInstance/ExitInstance 中创建/结束线程,
参见 http://support.microsoft.com/default.aspx?scid=kb;EN-US;142243获取更多信息。另请参阅 http://msdn.microsoft.com/en-us/library/ms682583%28VS.85%29.aspx关于 DllMain 的总体情况。
关于c++ - boost 线程卡在 _endthreadex,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2603583/