0
#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);
Gank
  • 4,507
  • 4
  • 49
  • 45

3 Answers3

2

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);
Zhi Wang
  • 1,138
  • 4
  • 20
  • 34
  • 1
    what kind of failure? Does the program not halt on the ~WaitForMultipleObjects~? Please add prints in the respective thread function, and see if the threads ran in very short periods. – Zhi Wang Jul 05 '13 at 09:11
  • I've edited the question.I want all threads's work done,then I can do more things. – Gank Jul 05 '13 at 09:26
1

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.

Community
  • 1
  • 1
doctorlove
  • 18,872
  • 2
  • 46
  • 62
0

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;
}
Christopher Janzon
  • 1,039
  • 11
  • 22