父线程和子线程 [英] parent and child thread

查看:106
本文介绍了父线程和子线程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果应用程序具有子线程变量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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆