pthread_cond_broadcast 不起作用?

发布于 2024-12-02 18:26:59 字数 1305 浏览 0 评论 0原文

我写了这个程序:

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) 不应该唤醒所有线程???

I have write this program:

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

But the output of the program is

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

pthread_cond_broadcast(&placeFootCondition) should not wake up all threads???

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

一紙繁鸢 2024-12-09 18:26:59

这就是你正在做的事情:

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_timedwait() 的特定互斥体
pthread_cond_wait()操作,之间形成动态绑定
互斥锁和条件变量只要在
至少有一个线程被条件变量阻塞。在此期间
时间,任何线程尝试等待该条件的效果
使用不同互斥体的变量未定义。



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

Here is what you are doing:

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

pthread_cond_wait(&placeFootCondition,&lf); /* Second thread uses lf mutex. */

Here is what the standard says about using different mutexes:

When a thread waits on a condition variable, having specified a
particular mutex to either the pthread_cond_timedwait() or the
pthread_cond_wait() operation, a dynamic binding is formed between
that mutex and condition variable that remains in effect as long as at
least one thread is blocked on the condition variable. During this
time, the effect of an attempt by any thread to wait on that condition
variable using a different mutex is undefined
.


Bottom line, from every thread you should use the same mutex when waiting on a condition variable.

神魇的王 2024-12-09 18:26:59

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

pthread_cond_wait 函数尽管有其名称,但它并不是一个条件等待。这是无条件等待某个条件。仅当需要等待条件时才必须调用它。

假设你和你姐姐共用一辆车,而你姐姐借了车,你就不能使用这辆车,所以你等着你姐姐回家。好吧,如果她已经回家了,你就要等很长时间了! (如果她回家了,但当你到达车旁时,她和车都不见了,你必须再次等待。)

模式是(对于你):

// let's get the car
pthread_mutex_lock(&mutex_that_protects_car);
while(car==SISTER_HAS_CAR) // car is gone
 pthread_mutex_wait(&condition_variable, &mutex_that_protects_car);
car=BROTHER_HAS_CAR; // it's my car now
pthread_mutex_unlock(&mutex_that_protects_car);
// we now have the car

对于你的妹妹:

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

请注意,必须保护汽车的状况由单个共享互斥体。汽车的状态是谓词,它受互斥体保护并由线程共享。

另请注意,我们在且仅当等待条件时才调用 pthread_cond_wait。当汽车空闲时,我们不会等待。我们一直在等待,以防她在我们抢到车之前再次抢到车。

In addition to the incorrect use of different mutexes, your program follows no logic. What is the condition the condition variable indicates? (This is called the 'predicate'.) Without a predicate, you will get lost wakeups.

The pthread_cond_wait function, despite its name, is not a conditional wait. It is an unconditional wait for a condition. You must call it only when the condition is to be waited for.

Suppose you and your sister share a car, and you can't use the car when your sister has borrowed it, so you wait for your sister to come home. Well, if she's already home, you're going to be waiting a long time! (And if she comes home but by the time you get to the car she and the car are gone, you have to wait again.)

The pattern is (for you):

// let's get the car
pthread_mutex_lock(&mutex_that_protects_car);
while(car==SISTER_HAS_CAR) // car is gone
 pthread_mutex_wait(&condition_variable, &mutex_that_protects_car);
car=BROTHER_HAS_CAR; // it's my car now
pthread_mutex_unlock(&mutex_that_protects_car);
// we now have the car

and for your sister:

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

Notice that the car's condition must be protected by a single, shared mutex. The state of the car is the predicate, and it is protected by the mutex and shared by the threads.

Notice also that we call pthread_cond_wait while, and only when, the condition is to be waited for. We do not wait when the car is free. And we keep waiting in case she grabbed the car again before we could grab it.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文