返回WaitForMultipleObject() [英] return of 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?
推荐答案
AfxBeginThread返回一个CWinThread对象,该对象在线程过程返回时删除自身.
来自MSDN:
dwCreateFlags
指定一个控制线程创建的附加标志.该标志可以包含两个值之一:
CREATE_SUSPENDED以挂起计数1启动线程.如果要在线程开始运行之前初始化CWinThread对象的任何成员数据(例如m_bAutoDelete或派生类的任何成员),请使用CREATE_SUSPENDED.初始化完成后,使用CWinThread :: ResumeThread启动线程运行.在调用CWinThread :: ResumeThread之前,线程不会执行.
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.
上面的循环会覆盖您的vars,
因此您将无法引用创建的线程...
您如何确切使用功能WaitForMultipleObjects(..)? :)
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 ? :)
是否还有其他方法可以分配不固定的子线程变量,这些变量将取决于运行时...
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;
}
}
实际上不考虑线程的子父关系. :)
主线程和辅助线程是一个更好的描述.
好吧,WaitForMultipleObjects本身就足够了,但是您已经提到了线程数,以便等待所有线程退出:)
但是只需遍历以下链接中提到的此代码段即可,该示例代码等待所有线程终止 ^ ]
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 [^]
这篇关于返回WaitForMultipleObject()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!