您的位置:程序门 -> vc/mfc -> 进程/线程/dll



请教 多线程编程问题?


[收藏此页] [打印本页]选择字色:背景色:字体:[][][]


请教 多线程编程问题?
发表于:2008-02-13 11:22:23 楼主
    我在应用程序中开启了3个线程,分别为线程a,线程b,线程c,我要在线程a和线程b执行完毕后,线程c才开始执行,我在mfc下如何去实现呢?
发表于:2008-02-13 11:34:001楼 得分:0
semaphore   用这个,hsemaphore=createsemaphore(null,2,2,null);  
保证每次只有2个线程在跑,可以去看看信标。
发表于:2008-02-13 11:46:552楼 得分:0

我在应用程序的代码如下:
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的程序
       
        这里的代码该如何实现
}
发表于:2008-02-13 11:52:023楼 得分:0
最简单的线程同步应用,用waitformultipleobjects等待两个线程句柄或者用两行waitforsingleobject函数分别等待即可。
发表于:2008-02-13 11:57:074楼 得分:0
handle   hthread[2];
hthread[0]   =   createthread(...);   //   开始线程a

hthread[1]   =   createthread(...);   //   开始线程b

::waitformultipleobjects(2,   &hthread,   true,   infinite);

createthread(...);   //   开始线程c
发表于:2008-02-13 12:03:535楼 得分:0
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);
}
发表于:2008-02-13 12:17:386楼 得分:0
我能否按如下的方式去做
说明: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执行
}  


发表于:2008-02-13 12:24:457楼 得分:0
只要对话框别关,看起来没问题。
发表于:2008-02-13 12:56:248楼 得分:0
使用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执行  
}      
发表于:2008-02-26 15:55:189楼 得分:0
int   g   =   0   ;

线程a

g   ++;
return;

线程b

g   ++;
return;

线程c
while(g   !=   2);
...
return;

----------------------
也可以吧!!!!




快速检索

最新资讯
热门点击