pthread_cond_广播不工作?

pthread_cond_广播不工作?,c,pthreads,C,Pthreads,我写了这个程序: pthread_cond_t placeFootCondition; pthread_mutex_t rf,lf; void* rss(void* in){ while(1){ pthread_mutex_lock(&rf); printf ( "rss: waiting for condition\n" ); pthread_cond_wait(&placeFootCondition,&rf)

我写了这个程序:

pthread_cond_t placeFootCondition;

pthread_mutex_t rf,lf;

void* rss(void* in){
    while(1){
        pthread_mutex_lock(&rf);
        printf ( "rss: waiting for condition\n" );
        pthread_cond_wait(&placeFootCondition,&rf);
        printf ( "                  Right Single Support \n" );
        sleep(1);
        pthread_mutex_unlock(&rf);

    }
}

void* lss(void* in){
    while(1){
        pthread_mutex_lock(&lf);
        printf ( "lss: waiting for condition\n" );
        pthread_cond_wait(&placeFootCondition,&lf);
        printf ( "Left Single Support \n" );
        sleep(1);
        pthread_mutex_unlock(&lf);

    } 
}


int main(){
    int rc;
    pthread_mutex_init(&rf,NULL);
    pthread_mutex_init(&lf,NULL);
    pthread_cond_init(&placeFootCondition,NULL);
    pthread_create(&t1,NULL,rss,NULL);
    pthread_create(&t2,NULL,lss,NULL);
    sleep(1);
    rc=pthread_cond_broadcast(&placeFootCondition);
    if(rc!=0) printf ( "rc=%i\n",rc );
    sleep(5);
}
但是程序的输出是

rss: waiting for condition
lss: waiting for condition
              Right Single Support 
rss: waiting for condition

pthread_cond_broadcast(&placeFootCondition)不应唤醒所有线程???

以下是您正在执行的操作:

pthread_cond_wait(&placeFootCondition,&rf); /* First thread uses rf mutex. */

pthread_cond_wait(&placeFootCondition,&lf); /* Second thread uses lf mutex. */
下面是关于使用不同互斥体的说明:

当线程在指定了 pthread_cond_timedwait()或 pthread_cond_wait()操作时,在 该互斥体和条件变量在 条件变量上至少有一个线程被阻塞。在此期间 时间,任何线程尝试在该条件下等待的效果 未定义使用不同互斥体的变量



底线是,在等待条件变量时,每个线程都应该使用相同的互斥锁。

以下是您的操作:

pthread_cond_wait(&placeFootCondition,&rf); /* First thread uses rf mutex. */

pthread_cond_wait(&placeFootCondition,&lf); /* Second thread uses lf mutex. */
下面是关于使用不同互斥体的说明:

当线程在指定了 pthread_cond_timedwait()或 pthread_cond_wait()操作时,在 该互斥体和条件变量在 条件变量上至少有一个线程被阻塞。在此期间 时间,任何线程尝试在该条件下等待的效果 未定义使用不同互斥体的变量



底线是,在等待条件变量时,每个线程都应该使用相同的互斥体。

除了不正确地使用不同的互斥体之外,您的程序不遵循任何逻辑。条件变量指示的条件是什么?(这称为“谓词”。)如果没有谓词,您将无法唤醒

pthread\u cond\u wait
函数,尽管名称不同,但不是条件等待。这是对某一条件的无条件等待。您必须仅在等待条件时调用它

假设你和你姐姐共用一辆车,而你姐姐借了你的车,你就不能用了,所以你就等着你姐姐回家。好吧,如果她已经在家了,你要等很长时间!(如果她回家了,但等你上车时她和车都走了,你就得再等一次。)

模式是(为您):

还有你妹妹:

// we are done with the car, make it free
pthread_mutex_lock(&mutex_that_protects_car);
car=CAR_IS_FREE;
pthread_cond_broadcast(&condition_variable); // he can have the car
pthread_mutex_unlock(&mutex_that_protects_car);
请注意,必须使用一个共享互斥锁来保护汽车的状况。car的状态是谓词,它由互斥锁保护并由线程共享


还要注意,我们在等待条件时调用
pthread\u cond\u wait
。汽车空闲时我们不等待。我们一直在等待,以防她在我们抓到它之前再次抓到它。

除了不正确地使用不同的互斥锁外,您的程序没有遵循任何逻辑。条件变量指示的条件是什么?(这称为“谓词”。)如果没有谓词,您将无法唤醒

pthread\u cond\u wait
函数,尽管名称不同,但不是条件等待。这是对某一条件的无条件等待。您必须仅在等待条件时调用它

假设你和你姐姐共用一辆车,而你姐姐借了你的车,你就不能用了,所以你就等着你姐姐回家。好吧,如果她已经在家了,你要等很长时间!(如果她回家了,但等你上车时她和车都走了,你就得再等一次。)

模式是(为您):

还有你妹妹:

// we are done with the car, make it free
pthread_mutex_lock(&mutex_that_protects_car);
car=CAR_IS_FREE;
pthread_cond_broadcast(&condition_variable); // he can have the car
pthread_mutex_unlock(&mutex_that_protects_car);
请注意,必须使用一个共享互斥锁来保护汽车的状况。car的状态是谓词,它由互斥锁保护并由线程共享

还要注意,我们在等待条件时调用
pthread\u cond\u wait
。汽车空闲时我们不等待。我们一直在等待,以防她在我们抓住汽车之前再次抓住它