工作者线程怎么还有这么玩的,各位快看看,解释一下!!
最近刚开始写 dialogic 语音卡的应用,在他的例子程序中有一个 mutil-threads voice的例程,
他是mdi 的应用程序,两个线程,一个主线程用于界面,一个worker thread用于语音卡控制,
我看了看,对它的线程结束机制很费解
他的工作者线程在程序启动时启动:
int PASCAL WinMain(HANDLE hInstance, HANDLE hPrevInstance,
LPSTR lpszCmdParam, int nCmdShow)
{
...
...
// create worker thread
CreateThread(NULL, 0, WorkerThread, (LPVOID)&x, 0, &dwThreadId); hAccelTable = LoadAccelerators(hInstance, szAppName); while(GetMessage(&msg, NULL, 0, 0)) {
if (!TranslateAccelerator(hwndFrame, hAccelTable, &msg)) {
TranslateMessage(&msg); // Translates virtual key codes
DispatchMessage(&msg); // Dispatches message to window
}
}
}
工作者线程如下:
DWORD WINAPI WorkerThread(LPVOID lpvThreadParm)
{
...
while(1)
{
if (sr_waitevt(-1) == -1) {//
WinPrintf("sr_waitevt: %s",ATDV_ERRMSGP(SRL_DEVICE));
return 1L;
}
...
... }
return 0;
}
sr_waitevt(-1) 是板卡驱动提供的库函数,意思是等待板卡的事件知道有事件产生
我注意到
while 死循环中除了一个出错处理外,其余没有一个 return点菜单的退出,程序能正常退出,退出处理只有一行
long PASCAL FrameWndProc(HWND hwnd, UINT message,
UINT wParam, LONG lParam)
{
...
switch(message) {
...
case IDM_FILE_EXIT:
PostQuitMessage(0);
return 0;
...
}按我的推论,当点退出时,程序窗口由于PostQuitMessage(0)能正常销毁,但是由于work thread没有退出,所以进程是没有销毁的。但是事实不是这样,各位,谁给解释一下
最近刚开始写 dialogic 语音卡的应用,在他的例子程序中有一个 mutil-threads voice的例程,
他是mdi 的应用程序,两个线程,一个主线程用于界面,一个worker thread用于语音卡控制,
我看了看,对它的线程结束机制很费解
他的工作者线程在程序启动时启动:
int PASCAL WinMain(HANDLE hInstance, HANDLE hPrevInstance,
LPSTR lpszCmdParam, int nCmdShow)
{
...
...
// create worker thread
CreateThread(NULL, 0, WorkerThread, (LPVOID)&x, 0, &dwThreadId); hAccelTable = LoadAccelerators(hInstance, szAppName); while(GetMessage(&msg, NULL, 0, 0)) {
if (!TranslateAccelerator(hwndFrame, hAccelTable, &msg)) {
TranslateMessage(&msg); // Translates virtual key codes
DispatchMessage(&msg); // Dispatches message to window
}
}
}
工作者线程如下:
DWORD WINAPI WorkerThread(LPVOID lpvThreadParm)
{
...
while(1)
{
if (sr_waitevt(-1) == -1) {//
WinPrintf("sr_waitevt: %s",ATDV_ERRMSGP(SRL_DEVICE));
return 1L;
}
...
... }
return 0;
}
sr_waitevt(-1) 是板卡驱动提供的库函数,意思是等待板卡的事件知道有事件产生
我注意到
while 死循环中除了一个出错处理外,其余没有一个 return点菜单的退出,程序能正常退出,退出处理只有一行
long PASCAL FrameWndProc(HWND hwnd, UINT message,
UINT wParam, LONG lParam)
{
...
switch(message) {
...
case IDM_FILE_EXIT:
PostQuitMessage(0);
return 0;
...
}按我的推论,当点退出时,程序窗口由于PostQuitMessage(0)能正常销毁,但是由于work thread没有退出,所以进程是没有销毁的。但是事实不是这样,各位,谁给解释一下
没有break while是不出奇的事情,当一个程序的主线程都退出后,它的子线程无论有没有退出也被强行退出。如果是用AfxBeginThread那就会内存泄漏,但例子中只用了CreateThread。这是编写人比较马虎的做法吧,反正只是个例子也没什么出错的。
有什么办法能解决呢?
在debug 模式下有一个expection 只不过是没有报上来,如下:
Loaded 'E:\WINNT\system32\msintf.dll', no matching symbolic information found.
Loaded 'E:\WINNT\system32\voxspan.dll', no matching symbolic information found.
The thread 0x3AC has exited with code 0 (0x0).
The thread 0xA0C has exited with code 0 (0x0).
The thread 0x7B8 has exited with code 0 (0x0).
The thread 0x83C has exited with code 0 (0x0).
DMR4CLEO: Process Detach
First-chance exception in Sample.exe (NTDLL.DLL): 0xC0000005: Access Violation.
The thread 0xAB0 has exited with code 0 (0x0).
The program 'F:\Dialogic\SAMPLES\VOICE\Sample.exe' has exited with code 0 (0x0).
把while(1)->while(!m_bCloseThread ), 主线程WaitSingleObject(hWorkThreadHandle, 500); // 等它个500毫秒之后才退也主程序。还有一个问题就是sr_waitevt(-1)中的-1是不是无限期等待?如果是就把它改为指定的时间值如100这样WorkThread才有机会去判断m_bCloseThread是否为TRUE,如果不是那就比较麻烦罗。