简体   繁体   中英

C++ Wait all threads to finish

#include <stdio.h>
#include <process.h>
#include <wtypes.h>
typedef unsigned int (__stdcall * THREAD_FUN_TYPE)(void *);

int ThreadIp(void* param)
{
    while(true)
    {
        printf("I'm runing!\n");
    }
    return 0;
}


int main()
{
    int iThreadNum=100;
    HANDLE* phThreads = new HANDLE[iThreadNum];
    for (int i=0;i<iThreadNum;++i)
    {
        phThreads[i]=(HANDLE*)_beginthreadex(NULL, 0,  (THREAD_FUN_TYPE)ThreadIp,NULL, NULL, NULL);
    }

    int nIndex = ::WaitForMultipleObjects(iThreadNum,phThreads,1,INFINITE);
    printf("End!\n");
    return 0;
}

I want the program will halt at WaitForMultipleObjects until all thread are end (Not until all thread are created successfully).But the program will not halt at WaitForMultipleObjects,while all threads are still running. So I try to use SetEvent,but still the same problem:

int iThreadNum=100;
HANDLE* phThreads = new HANDLE[iThreadNum];
for (int i=0;i<iThreadNum;++i)
{
    phThreads[i]=CreateEvent(NULL, FALSE, FALSE,NULL);
    ResetEvent(phThreads[i]);
}
int nIndex = ::WaitForMultipleObjects(iThreadNum,phThreads,1,INFINITE);

You should wait on the thread handles, not the unrelated events:

Try something like this:

 int iThreadNum=100;
 HANDLE* phThreads = new HANDLE[iThreadNum];
 for (int i=0;i<iThreadNum;++i)
 {
     m_iCurThreadNum=i;
     phThreads[i] = _beginthreadex(...);
  }

 int nIndex = ::WaitForMultipleObjects(iThreadNum,phThreads,1,INFINITE);

Does it work if you have fewer threads? The manual says you need to do extra work if you have more than MAXIMUM_WAIT_OBJECTS , specifically

nCount [in] The number of object handles in the array pointed to by lpHandles. The maximum number of object handles is MAXIMUM_WAIT_OBJECTS. This parameter cannot be zero.

See here for a discussion.

It might be worth checking what the wait function has returned too.

I would allocate a struct before calling _beginthreadex and pass the pointer to the struct through the threads parameter and have the struct contain a bool which is set by the thread when it's done.

struct ThreadStruct{
    bool Done;
    char* ParamData;
    int ParamDataSize;
};

int ThreadIp(void* param)
{
    ThreadStruct* ts = (ThreadStruct*)param;
    while(true)
    {
        printf("I'm runing!\n");
    }
    ts->Done = true;
    return 0;
}

int main()
{
    int iThreadNum=100;
    HANDLE* phThreads = new HANDLE[iThreadNum];
    ThreadStruct* structs = new ThreadStruct[iThreadNum];
    for (int i=0;i<iThreadNum;++i)
    {
        ZeroMemory(structs[i], sizeof(ThreadStruct));
        phThreads[i]=(HANDLE*)_beginthreadex(NULL, 0,  (THREAD_FUN_TYPE)ThreadIp, structs[i], NULL, NULL);
        ResetEvent(phThreads[i]);
    }

    for(unsigned int i=0; i<iThreadNum;){
        if(!structs[i]->Done) i=0;
        else i++;
    }
    printf("End!\n");
    return 0;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM