父线程和子线程 [英] parent and child thread
问题描述
如果应用程序具有子线程变量no,那么如何分配子线程的引用变量,因为没有固定的子线程no,以及父线程如何等待子线程no的所有终止之前终止父终止??
我正在使用信号量在子线程之间进行同步.
CWinThread * Thread [2];
CSemaphore cs(2,2);
..........
UINT ParentThread()
{
........
......
while(cond == true)
{
线程[0] = AfxBeginThread(ChildThread1,LPVOID(NULL));
线程[1] = afxBeginThread(ChildThread2,LPVOID(NULL));
}
.......
.....
}
UINT ChildThread1()
{
CSingleLock Lock();
....
Unlock();
}
UINT ChildThread2()
{
CSingleLock Lock();
....
UnLock();
}
If an application having the variable no of child thread then how the refernce variable for child thread are assigned ,since there are not fixed no of child thread and how the parent thread wait for all the variable no of child thread to terminate before the parent termination??
I m using Semaphore for synchronization between child thread.
CWinThread *Thread[2];
CSemaphore cs(2,2);
..........
UINT ParentThread()
{
........
......
while(cond==true)
{
Thread[0]=AfxBeginThread(ChildThread1,LPVOID(NULL));
Thread[1]=afxBeginThread(ChildThread2,LPVOID(NULL));
}
.......
.....
}
UINT ChildThread1()
{
CSingleLock Lock();
....
Unlock();
}
UINT ChildThread2()
{
CSingleLock Lock();
....
UnLock();
}
推荐答案
构建一个包含(活动)线程句柄的数组,并使用WaitForMultipleObjects
[由EP编辑]
对!
或者,例如指针数组 [
Build an array that includes your (active) thread handles and use WaitForMultipleObjects
Right !
Or, for eample A pointer array[^] ,
that would hold the result pointers of AfxBeginThread(..) :)
这篇关于父线程和子线程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!