为什么我们需要在pthread_cond_wait之前进行条件检查 [英] Why do we need a condition check before pthread_cond_wait

查看:146
本文介绍了为什么我们需要在pthread_cond_wait之前进行条件检查的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试学习pthread_cond_wait的基础知识.在所有用法中,我都可以看到

I am trying to learn basics of pthread_cond_wait. In all the usages, I see either

if(cond is false)
   pthread_cond_wait

while(cond is false)
   pthread_cond_wait

我的问题是,我们只想cond_wait因为条件为假.那我为什么要忍受明确地放置一个if/while循环的痛苦.我可以理解,在cond_wait之前不进行任何if/while检查,我们将直接将其击中,它根本不会返回.条件检查是仅用于解决此目的,还是具有其他意义.如果它用于解决不必要的条件等待,则进行条件检查并避免cond_wait类似于轮询?我正在这样使用cond_wait.

My question is, we want to cond_wait only because condition is false. Then why should i take the pain of explicitly putting an if/while loop. I can understand that without any if/while check before cond_wait we will directly hit that and it wont return at all. Is the condition check solely for solving this purpose or does it have anyother significance. If it for solving an unnecessary condition wait, then putting a condition check and avoiding the cond_wait is similar to polling?? I am using cond_wait like this.

void* proc_add(void *name){
    struct vars *my_data = (struct vars*)name;
    printf("In thread Addition and my id = %d\n",pthread_self());
    while(1){
    pthread_mutex_lock(&mutexattr);
    while(!my_data->ipt){  // If no input get in
            pthread_cond_wait(&mutexaddr_add,&mutexattr);  // Wait till signalled
            my_data->opt = my_data->a + my_data->b;
            my_data->ipt=1;
            pthread_cond_signal(&mutexaddr_opt);
    }
    pthread_mutex_unlock(&mutexattr);
    if(my_data->end)
            pthread_exit((void *)0);
    }
}

逻辑是,我要求输入线程在有可用输入时处理数据,并通知输出线程进行打印.

The logic is, I am asking the input thread to process the data whenever an input is available and signal the output thread to print it.

推荐答案

您需要一个while循环,因为即使未达到您等待的条件,调用pthread_cond_wait的线程也可能会唤醒.这种现象称为虚假唤醒".

You need a while loop because the thread that called pthread_cond_wait might wake up even when the condition you are waiting for isn't reached. This phenomenon is called "spurious wakeup".

这不是错误,它是条件变量的实现方式.

This is not a bug, it is the way the conditional variables are implemented.

这也可以在手册页中找到:

This can also be found in man pages:

从pthread_cond_timedwait()虚假唤醒 pthread_cond_wait()函数可能会发生.自从归来 pthread_cond_timedwait()或pthread_cond_wait()并不暗示 关于此谓词的值的任何内容,谓词应为 这样的回报会重新评估.

Spurious wakeups from the pthread_cond_timedwait() or pthread_cond_wait() functions may occur. Since the return from pthread_cond_timedwait() or pthread_cond_wait() does not imply anything about the value of this predicate, the predicate should be re-evaluated upon such return.

有关实际代码的更新:

void* proc_add(void *name) 
{
    struct vars *my_data = (struct vars*)name;

    printf("In thread Addition and my id = %d\n",pthread_self());

    while(1) {

        pthread_mutex_lock(&mutexattr);

        while(!my_data->ipt){  // If no input get in
            pthread_cond_wait(&mutexaddr_add,&mutexattr);  // Wait till signalled
        }

        my_data->opt = my_data->a + my_data->b;
        my_data->ipt=1;
        pthread_cond_signal(&mutexaddr_opt);

        pthread_mutex_unlock(&mutexattr);

        if(my_data->end)
            pthread_exit((void *)0);
        }
    }
}

这篇关于为什么我们需要在pthread_cond_wait之前进行条件检查的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆