我在应用程序中开启了3个线程,分别为线程A,线程B,线程C,我要在线程A和线程B执行完毕后,线程C才开始执行,我在MFC下如何去实现呢?

解决方案 »

  1.   

    Semaphore 用这个,hSemaphore=CreateSemaphore(NULL,2,2,NULL); 
    保证每次只有2个线程在跑,可以去看看信标。
      

  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的程序
        
        这里的代码该如何实现
    }
      

  3.   

    最简单的线程同步应用,用WaitForMultipleObjects等待两个线程句柄或者用两行WaitForSingleObject函数分别等待即可。
      

  4.   

    HANDLE hThread[2];
    hThread[0] = CreateThread(...); // 开始线程AhThread[1] = CreateThread(...); // 开始线程B::WaitForMultipleObjects(2, &hThread, TRUE, INFINITE);CreateThread(...); // 开始线程C
      

  5.   

    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);
    }
      

  6.   

    我能否按如下的方式去做
    说明: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执行

      

  7.   

    使用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执行 
    }   
      

  8.   

    int g = 0 ;线程ag ++;
    return;线程bg ++;
    return;线程c
    while(g != 2);
    ...
    return;----------------------
    也可以吧!!!!