我在主线程中创建10个工作线程,然后调用WaitForMultipleObjects等待其中的一个成功返回,线程函数做的工作很简单(为了测试),在WaitForMultipleObjects这儿就阻塞了
得不到工作线程的通知,设置超时,就一直返回超时,为什么?
主线程在一个Button的click事件中.源码如下:
void testFuncs()
{
HANDLE hThread[10];
DWORD threadID,dw;
CString temp;         InitializeCriticalSection(&g_cs); for(int i=0;i<100;i++)
{
hThread[i] = CreateThread   (NULL,0,LPTHREAD_START_ROUTINE) testFuncProc,NULL,0,&threadID);
if(!hThread[i])
{
g_testStruct.time = 0.0;
temp.Format("%s%d%s","Create thread ",i,"error!");
g_testStruct.str += temp;
}
} dw = WaitForMultipleObjects(threads,hThread,FALSE,50000);
if(dw == WAIT_FAILED)
{
g_testStruct.time = 0.0;
temp.Format("%s%d","Wait all Thread failure!");
g_testStruct.str += temp;
break;
 }
else if (dw == WAIT_TIMEOUT)
{
g_testStruct.time = 0.0;
temp.Format("%s","Wait all Thread timeout!");
g_testStruct.str += temp;
    break;
}
else
{
threadNum = (int)(dw-WAIT_OBJECT_0+1);
temp.Format("%s%d%s","Thread",threadNum,g_testStruct.str);
g_testStruct.str = temp;
threadCounts--;
}
} return;
}DWORD WINAPI testFuncProc(LPVOID )
{
EnterCriticalSection(&g_cs);
if(g_funcNo>=3)
g_funcNo = 0; g_testStruct.time = g_funcNo;
g_testStruct.str = "This is test by tongchun!";
         LeaveCriticalSection(&g_cs);
}

解决方案 »

  1.   

    是不是应该加入这样的一段代码: MSG msg;
    while(TRUE)
    {
    DWORD dwRet = WaitForSingleObject(m_hLoadThread,50);//自己改成WaitForMultipleObjects
    if(dwRet != WAIT_TIMEOUT)
    break;
    if(GetMessage(&msg,NULL,NULL,NULL))
    {
    TranslateMessage(&msg);
    DispatchMessage(&msg);
    }
    }

    CloseHandle(m_hLoadThread);//如果不是所需要的,不能挡住WINDOWS里面的消息
      

  2.   

    for(int i=0;i<100;i++)  ??敲错了吗?
      

  3.   

    threadNum = (int)(dw-WAIT_OBJECT_0+1);
    temp.Format("%s%d%s","Thread",threadNum,g_testStruct.str);
    g_testStruct.str = temp;
    threadCounts--;
    呵呵,这个  threadCounts--;也有问题。自己想想为什么吧。
      

  4.   

    主线程在一个Button的click事件中,我点击Button,调用一
    函数,然后产生多个线程,等待其中的一个成功.再进行下一步
    操作.
      

  5.   

    源码如下:
    void testFuncs()
    {
    HANDLE hThread[10];
    DWORD threadID,dw;
    CString temp;         InitializeCriticalSection(&g_cs); for(int i=0;i<10;i++)
    {
    hThread[i] = CreateThread   (NULL,0,LPTHREAD_START_ROUTINE) testFuncProc,NULL,0,&threadID);
    if(!hThread[i])
    {
    g_testStruct.time = 0.0;
    temp.Format("%s%d%s","Create thread ",i,"error!");
    g_testStruct.str += temp;
    }
    } dw = WaitForMultipleObjects(threads,hThread,FALSE,50000);
    if(dw == WAIT_FAILED)
    {
    g_testStruct.time = 0.0;
    temp.Format("%s%d","Wait all Thread failure!");
    g_testStruct.str += temp;
    break;
     }
    else if (dw == WAIT_TIMEOUT)
    {
    g_testStruct.time = 0.0;
    temp.Format("%s","Wait all Thread timeout!");
    g_testStruct.str += temp;
        break;
    }
    else
    {
    threadNum = (int)(dw-WAIT_OBJECT_0+1);
    temp.Format("%s%d%s","Thread",threadNum,g_testStruct.str);
    g_testStruct.str = temp;
    break;
    }
    } return;
    }DWORD WINAPI testFuncProc(LPVOID )
    {
    EnterCriticalSection(&g_cs);
    if(g_funcNo>=3)
    g_funcNo = 0; g_testStruct.time = g_funcNo;
    g_testStruct.str = "This is test by tongchun!";
             LeaveCriticalSection(&g_cs);
    }
    应该是这样,有些是敲错了.
    请看看!
      

  6.   

    没看出来问题,除了那个for(100),但阻塞一定不是这个原因
    WaitForMultipleObjects(threads,hThread,FALSE,50000);中的threads没错吧?
      

  7.   

    threads 就是100.
    关键问题是WaitForMultipleObjects等待任何一个hThread给他
    发状态通知,而线程内核对象是有这个特性的。而我的线程非常简单,
    不存在自身的问题。为什么等待不到?
      

  8.   

    改了一下代码,测试了一下,没有发现问题啊?CRITICAL_SECTION g_cs;
    char g_Data[128]  = {'\0'};////////////////////////////////////////////////////////////////////
    DWORD WINAPI testFuncProc(LPVOID lpv)
    {
    EnterCriticalSection(&g_cs); strcat(g_Data, "|Thread Accessed|"); LeaveCriticalSection(&g_cs); return 0;
    }/////////////////////////////////////////////////////////////////
    HANDLE hThread[10];
    DWORD ThreadID, dwWait;
    int i;InitializeCriticalSection(&g_cs);
    for( i=0; i<10; i++ )
    {
        hThread[i] = CreateThread(NULL,0,testFuncProc,NULL,0,&ThreadID);
    }for( i=0; i<10; )
    {
        dwWait = WaitForMultipleObjects(10,hThread,FALSE,50000);
        if(dwWait == WAIT_FAILED)
        {
    OutputDebugStringA("Wait Failed!\n");
        }
        else if (dwWait == WAIT_TIMEOUT)
        {
    OutputDebugStringA("Wait Timeout!\n");
        }
        else
        {
    char string[256];
    sprintf(string,"Thread %d end\n", (int)(dwWait-WAIT_OBJECT_0+1));
    OutputDebugStringA(string);
    i ++;
         }
    }DeleteCriticalSection(&g_cs);
    OutputDebugStringA(g_Data);所有线程都正确执行退出了,不过,输出很有意思,似乎WaitForMultipleObjects有Bug,我用的WinMe Ver4.90.3000.
    输出:
    The thread 0xFFF2DD57 has exited with code 0 (0x0).
    Thread 1 end
    Thread 1 end
    The thread 0xFFF2DF6B has exited with code 0 (0x0).
    Thread 1 end
    The thread 0xFFF2D13F has exited with code 0 (0x0).
    Thread 1 end
    The thread 0xFFF2D2D3 has exited with code 0 (0x0).
    Thread 1 end
    The thread 0xFFF2D4E7 has exited with code 0 (0x0).
    Thread 1 end
    The thread 0xFFF2D6BB has exited with code 0 (0x0).
    Thread 1 end
    The thread 0xFFF1282F has exited with code 0 (0x0).
    Thread 1 end
    The thread 0xFFF12DA3 has exited with code 0 (0x0).
    Thread 1 end
    The thread 0xFFF12ED7 has exited with code 0 (0x0).
    Thread 1 end
    The thread 0xFFF1204B has exited with code 0 (0x0).
    |Thread Accessed||Thread Accessed||Thread Accessed||Thread Accessed||Thread Accessed||Thread Accessed||Thread Accessed||Thread Accessed||Thread Accessed||Thread Accessed|
      

  9.   

    正确,是输出比较慢的原因。
    在unix下printf输出也常出现这种现象,正常
      

  10.   

    正确,是输出比较慢的原因。
    在unix下printf输出也常出现这种现象,正常
      

  11.   

    不是这个,我是指所有返回值都是WAIT_OBJECT_0,结果总是输出
    Thread 1 end
      

  12.   

    to  In355Hz(好象一条狗) 当然啦,1号线程先退出了,hThread[0]有信号了,以后每次它都是有信号的,所以每次都是1。
    如果3号线程先退出,然后是1号,结果如下:
    Thread 3 end
    Thread 1 end
    Thread 1 end
    Thread 1 end
    与其它先程无关,即使其它线程阻塞在里面,所以你这段代码不严谨
      

  13.   

    对了,考虑错了,其实第二次调用时Thread[0]已经是由信号的了,应该重新填充Thread数组。
    多谢提醒。