本文介绍了返回WaitForMultipleObject()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在基于MFC对话框的应用程序中,有一个父线程,并且在这个父线程中,我有多个(不是固定数量的)子线程.
子线程指针被声明为CWinThread *Thread[2];,并在类似以下的循环中使用:

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));
}


上面的while循环没有迭代.时代.
我正在使用WaitForMultipleObject()等待所有子线程发出信号,然后终止父线程.
WaitForMultipleObjects的返回始终为-1.


子线程变量会被覆盖吗?
是否还有其他方法可以分配不固定的子线程变量,这些变量将取决于运行时...

请帮忙!


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?

推荐答案


sksksksksksksks写道:
sksksksksksksks wrote:

是否还有其他方法可以分配不固定的子线程变量,这些变量将取决于运行时...

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



您需要多少个线程?
如果它们只有两个,则不需要循环:)

假设您的var有效:



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;
  }
}



这篇关于返回WaitForMultipleObject()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-29 17:13