我想我犯了一个简单的错误,但是因为我注意到这里有很多助推专家,我以为我会寻求帮助。
我正在尝试在Windows XP上使用boost thread(1_40)。主程序加载一个dll,像这样启动线程(注意这不在一个类中,静态并不意味着静态到一个类而是私有的文件)。
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");
}
}
问题是主线程挂在连接上,网络线程挂在_endthreadex的末尾。我误解了什么?
答案 0 :(得分:3)
您不应该在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的一般信息。