C++ 无法理解pthread\u cond\u wait/signal/broadcast

C++ 无法理解pthread\u cond\u wait/signal/broadcast,c++,multithreading,pthreads,C++,Multithreading,Pthreads,我试图理解pthread的条件。 为此,我写了一个非常基本的程序,但无论我读了多少手册和解释,我都无法理解事情是如何运作的 #include <iostream> #include <pthread.h> using namespace std; #define NT 3 #define ITERATIONS 5 int countDone=0; int a[NT] = {10, 20, 30}; pthread_cond_t doneCond = PTHREAD_CO

我试图理解pthread的条件。 为此,我写了一个非常基本的程序,但无论我读了多少手册和解释,我都无法理解事情是如何运作的

#include <iostream>
#include <pthread.h>
using namespace std;
#define NT 3
#define ITERATIONS 5

int countDone=0;
int a[NT] = {10, 20, 30};
pthread_cond_t doneCond = PTHREAD_COND_INITIALIZER;
pthread_mutex_t doneMutex = PTHREAD_MUTEX_INITIALIZER;

void * thread_start(void* num){
    int currIt=0;

    while(currIt < ITERATIONS){
    cout << pthread_self() << "my number is " << a[(int)num] << endl;
    a[(int)num]++;

    pthread_mutex_lock(&doneMutex);
    countDone++;

    if(countDone == NT)
        pthread_cond_signal(&doneCond);

        while(countDone != 0)
            pthread_cond_wait(&doneCond, &doneMutex);
    pthread_mutex_unlock(&doneMutex);
    currIt++;
    }   
    pthread_exit(NULL);
}

int main(){
    pthread_t tidVec[NT];
    int i, currIt=0, ret;

    //creating threads
    for(i=0; i<NT; i++)
        pthread_create(&tidVec[i], NULL, &thread_start, (void*)i);

    while(currIt < ITERATIONS){ 
        cout << "---Iteration " << currIt << endl;  
        pthread_mutex_lock(&doneMutex);

        while(countDone < NT)
            pthread_cond_wait(&doneCond, &doneMutex);

        countDone = 0;
        pthread_cond_broadcast(&doneCond);
        pthread_mutex_unlock(&doneMutex);
        currIt++;
    }

    //waiting for threads termination
    for(i=0; i<NT; i++)
        pthread_join(tidVec[i], (void**)&ret);
    pthread_cond_destroy(&doneCond);
    pthread_mutex_destroy(&doneMutex);
    return 0;
}
然后它就卡住了。 你能帮我弄清楚这是怎么回事吗


谢谢。

您从未将
pthread\u mutex\u t
pthread\u cond\u t
初始化为有效状态。您是对的,我已修复,但问题仍然存在。感谢您在
thread\u start()
中发出通知,如果(countDone==NT)pthread\u cond\u信号(&donsecond),您将收到
if(countDone==NT)pthread\u cond\u信号(&donsecond)向主线程发送信号。但是,此时,另一个线程可能正在等待
(countDone!=0)pthread_cond_wait(&donsecond,&doneMutex),在这种情况下,可能会唤醒其他线程而不是主线程。您可以选择:a)使用两个不同的
pthread\u cond\t
s,或者b)
pthread\u cond\u broadcast()
。使用2个pthread\u cond\t和2个pthread\u mutex它可以工作!但是我必须在(countDone!=0)
时用
如果(countDone!=0)
更改
,我希望这是正确的,我只是不走运:)谢谢@EOFQ.e.d.,我只是走运,有时有效,有时无效。我不知道还能做什么你从不将
pthread\u mutex\t
pthread\u cond\u t
初始化为有效状态。你是对的,我已修复,但问题仍然存在。感谢您在
thread\u start()
中发出通知,如果(countDone==NT)pthread\u cond\u信号(&donsecond),您将收到
if(countDone==NT)pthread\u cond\u信号(&donsecond)向主线程发送信号。但是,此时,另一个线程可能正在等待
(countDone!=0)pthread_cond_wait(&donsecond,&doneMutex),在这种情况下,可能会唤醒其他线程而不是主线程。您可以选择:a)使用两个不同的
pthread\u cond\t
s,或者b)
pthread\u cond\u broadcast()
。使用2个pthread\u cond\t和2个pthread\u mutex它可以工作!但是我必须在(countDone!=0)
时用
如果(countDone!=0)
更改
,我希望这是正确的,我只是不走运:)谢谢@EOFQ.e.d.,我只是走运,有时有效,有时无效。我不知道还能做什么
---Iteration 0
3057584960my number is 30
3065977664my number is 20
3074370368my number is 10
---Iteration 1
3057584960my number is 31