Click here to Skip to main content
15,891,184 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
In my MFC dialog-based application there is one parent thread and inside this parent thread I am having multiple (not a fixed number) of child threads.
The child thread pointers are declared as CWinThread *Thread[2]; and used in a loop like:
while(codn==true)
{
  Thread[0]=AfxBeginThread(Thread1,LPVOID(NULL));
  Thread[1]=AfxBeginThread(Thread2,LPVOID(NULL));
}

The above while loop iterates no. of times.
I m using WaitForMultipleObject() to wait for all child thread to signal before the termination of the parent thread.
The return of WaitForMultipleObjects is always -1.


The child thread variables gets overwritten?
Is there any other method to assign the child thread variables that are not fixed and will depends on runtime...

Please help! How can I wait for all the child threads to complete their tasks before terminating the parent thread?
Posted
Updated 25-Apr-10 22:16pm
v4

AfxBeginThread returns a CWinThread object, which deletes itself when the thread procedure returns.

From MSDN:
dwCreateFlags
Specifies an additional flag that controls the creation of the thread. This flag can contain one of two values:

CREATE_SUSPENDED Start the thread with a suspend count of one. Use CREATE_SUSPENDED if you want to initialize any member data of the CWinThread object, such as m_bAutoDelete or any members of your derived class, before the thread starts running. Once your initialization is complete, use CWinThread::ResumeThread to start the thread running. The thread will not execute until CWinThread::ResumeThread is called.
 
Share this answer
 
v2
The loop above does overwrite your vars,
so you will not can to reference the created threads...

How do you use the function WaitForMultipleObjects(..) exactly ? :)


sksksksksksksks wrote:
Is there any other method to assign the child thread variables that are not fixed and will depends on runtime...


How many threads do you need ?
If they are two only then you do not need the loop :)

Assumed your vars are valid:

void CYourDialog::EndWorkers()
{
  /*volatile bool*/ m_bExitThreads = true; // signal the threads
                                           // about the exiting
  if (m_Thread[0] && m_Thread[1]) {
    HANDLE hThreads[2] = {
      m_Thread[0]->m_hThread,
      m_Thread[1]->m_hThread
    };
 
    WaitForMultipleObjects(2,
                           hThreads,
                           TRUE,
                           INFINITE);
 
    m_Thread[0] = m_Thread[1] = NULL;
  }
}
 
Share this answer
 
v2
Actually dont consider child parent relation for a thread. :)
Main Thread and worker thread is a better description.
Well WaitForMultipleObjects itself it enough but you have mention the Thread count so that waits for the all threads to exit :)
But just go through this code snippet mentioned in below link, a sample code that waits for the all thread to terminate [^]
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900