请教 多线程编程问题? 我在应用程序中开启了3个线程,分别为线程A,线程B,线程C,我要在线程A和线程B执行完毕后,线程C才开始执行,我在MFC下如何去实现呢? 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 Semaphore 用这个,hSemaphore=CreateSemaphore(NULL,2,2,NULL); 保证每次只有2个线程在跑,可以去看看信标。 我在应用程序的代码如下:void mydlg::start_thread() { //开启通讯线程1 HANDLE hThread; hThread = CreateThread(NULL, 0, thread_proc_tongxun1, this, 0, NULL); CloseHandle(hThread); //开启通讯线程2 HANDLE hThread; hThread = CreateThread(NULL, 0, thread_proc_tongxun2, this, 0, NULL); CloseHandle(hThread); //开启通讯线程3 HANDLE hThread; hThread = CreateThread(NULL, 0, thread_proc_tongxun3, this, 0, NULL); CloseHandle(hThread);}DWORD WINAPI thread_proc_tongxun1(LPVOID lpParameter){ (mydlg *)lpParameter)->tongxun1 = 1;//线程1执行完的标志}DWORD WINAPI thread_proc_tongxun2(LPVOID lpParameter){ (mydlg *)lpParameter)->tongxun2 = 1;//线程2执行完的标志}DWORD WINAPI thread_proc_tongxun3(LPVOID lpParameter){ //要求线程3在线程1和线程2执行完毕后,才执行线程3的程序 这里的代码该如何实现} 最简单的线程同步应用,用WaitForMultipleObjects等待两个线程句柄或者用两行WaitForSingleObject函数分别等待即可。 HANDLE hThread[2];hThread[0] = CreateThread(...); // 开始线程AhThread[1] = CreateThread(...); // 开始线程B::WaitForMultipleObjects(2, &hThread, TRUE, INFINITE);CreateThread(...); // 开始线程C hSemaphore=CreateSemaphore(NULL,2,2,NULL); //创建信标内核对象 for(int i=0;i<=3;i++){//循环开3个线程 WaitForSingleObject(hSemaphore,INFINITE); //等待信标的计数器可用,保证每次只有2个线程 //等到一个可用资源,计数器减1 hThread = CreateThread(NULL,0,threadPro,(LPVOID)i,0,NULL); //在线程内部释放占用的资源,调用ReleaseSemaphore(hSemaphore,1,NULL); //使信标的计数器加1 Sleep(SleepTime); if(hThread!=NULL) CloseHandle(hThread); } 我能否按如下的方式去做说明:HANDLE hThread[2];是mydlg类的变量 void mydlg::start_thread() { //开启通讯线程1 hThread[0]=CreateThread(NULL, 0, thread_proc_tongxun1, this, 0, NULL); //开启通讯线程2 hThread[1]=CreateThread(NULL, 0, thread_proc_tongxun2, this, 0, NULL); //开启通讯线程3 HANDLE hThread hThread=(NULL, 0, thread_proc_tongxun3, this, 0, NULL); //调用一个非模态对话框,给用户显示一些提示信息 dlg_msg my_msg_Dlg;//定义界面提示对话框 my_msg_Dlg.DoModal();//显示对话框 CloseHandle(hThread[0]); CloseHandle(hThread[1]); CloseHandle(hThread);}DWORD WINAPI thread_proc_tongxun1(LPVOID lpParameter) { 线程1执行} DWORD WINAPI thread_proc_tongxun2(LPVOID lpParameter) { 线程2执行 } DWORD WINAPI thread_proc_tongxun3(LPVOID lpParameter) { //要求线程3在线程1和线程2执行完毕后,才执行线程3的程序 ::WaitForMultipleObjects(2, &hThread, TRUE, INFINITE); 线程3执行} 使用event事件通知同步,不用对话框。具体函数有点记不清,下面只是示意。event[2]DWORD WINAPI thread_proc_tongxun1(LPVOID lpParameter) { 线程1执行 event[0] = true; } DWORD WINAPI thread_proc_tongxun2(LPVOID lpParameter) { 线程2执行 event[1] = true;} DWORD WINAPI thread_proc_tongxun3(LPVOID lpParameter) { //要求线程3在线程1和线程2执行完毕后,才执行线程3的程序 ::WaitForMultipleObjects(2, &event, TRUE, INFINITE); 线程3执行 } int g = 0 ;线程ag ++;return;线程bg ++;return;线程cwhile(g != 2);...return;----------------------也可以吧!!!! 文件保存路径奇怪的问题 MFC 单文档程序如何添加、初始化CListCtrl控件? vc按函数值大小画图 单文档中显示对话框的问题,在线等解决~~ VC++6.0将磁盘.exe文件写入程序进程的地址空间内并执行之 ExtTextOut能输出指定颜色和字体的字符到指定HDC上吗? 调试出现的问题:内存不能为“written” 急急急!!!请问一个vc问题! 请问哪位知道WORD2000的文件格式,谢谢 我的CStatic上为什么不能贴位图? 请问福昕软件(北京)开发有限公司怎么样,大家有了解的么? 关闭对话框不成功
保证每次只有2个线程在跑,可以去看看信标。
我在应用程序的代码如下:
void mydlg::start_thread()
{
//开启通讯线程1
HANDLE hThread;
hThread = CreateThread(NULL, 0, thread_proc_tongxun1, this, 0, NULL);
CloseHandle(hThread); //开启通讯线程2
HANDLE hThread;
hThread = CreateThread(NULL, 0, thread_proc_tongxun2, this, 0, NULL);
CloseHandle(hThread); //开启通讯线程3
HANDLE hThread;
hThread = CreateThread(NULL, 0, thread_proc_tongxun3, this, 0, NULL);
CloseHandle(hThread);
}DWORD WINAPI thread_proc_tongxun1(LPVOID lpParameter)
{
(mydlg *)lpParameter)->tongxun1 = 1;//线程1执行完的标志
}
DWORD WINAPI thread_proc_tongxun2(LPVOID lpParameter)
{
(mydlg *)lpParameter)->tongxun2 = 1;//线程2执行完的标志}
DWORD WINAPI thread_proc_tongxun3(LPVOID lpParameter)
{
//要求线程3在线程1和线程2执行完毕后,才执行线程3的程序
这里的代码该如何实现
}
hThread[0] = CreateThread(...); // 开始线程AhThread[1] = CreateThread(...); // 开始线程B::WaitForMultipleObjects(2, &hThread, TRUE, INFINITE);CreateThread(...); // 开始线程C
for(int i=0;i<=3;i++){//循环开3个线程
WaitForSingleObject(hSemaphore,INFINITE);
//等待信标的计数器可用,保证每次只有2个线程
//等到一个可用资源,计数器减1
hThread = CreateThread(NULL,0,threadPro,(LPVOID)i,0,NULL);
//在线程内部释放占用的资源,调用ReleaseSemaphore(hSemaphore,1,NULL);
//使信标的计数器加1
Sleep(SleepTime);
if(hThread!=NULL)
CloseHandle(hThread);
}
说明:HANDLE hThread[2];是mydlg类的变量 void mydlg::start_thread()
{
//开启通讯线程1
hThread[0]=CreateThread(NULL, 0, thread_proc_tongxun1, this, 0, NULL); //开启通讯线程2
hThread[1]=CreateThread(NULL, 0, thread_proc_tongxun2, this, 0, NULL); //开启通讯线程3
HANDLE hThread
hThread=(NULL, 0, thread_proc_tongxun3, this, 0, NULL);
//调用一个非模态对话框,给用户显示一些提示信息
dlg_msg my_msg_Dlg;//定义界面提示对话框
my_msg_Dlg.DoModal();//显示对话框
CloseHandle(hThread[0]);
CloseHandle(hThread[1]);
CloseHandle(hThread);
}
DWORD WINAPI thread_proc_tongxun1(LPVOID lpParameter)
{
线程1执行
}
DWORD WINAPI thread_proc_tongxun2(LPVOID lpParameter)
{
线程2执行
} DWORD WINAPI thread_proc_tongxun3(LPVOID lpParameter)
{
//要求线程3在线程1和线程2执行完毕后,才执行线程3的程序
::WaitForMultipleObjects(2, &hThread, TRUE, INFINITE);
线程3执行
}
event[2]DWORD WINAPI thread_proc_tongxun1(LPVOID lpParameter)
{
线程1执行
event[0] = true;
}
DWORD WINAPI thread_proc_tongxun2(LPVOID lpParameter)
{
线程2执行
event[1] = true;
} DWORD WINAPI thread_proc_tongxun3(LPVOID lpParameter)
{
//要求线程3在线程1和线程2执行完毕后,才执行线程3的程序
::WaitForMultipleObjects(2, &event, TRUE, INFINITE);
线程3执行
}
return;线程bg ++;
return;线程c
while(g != 2);
...
return;----------------------
也可以吧!!!!