【问题标题】:When to use pthread condition variables?何时使用 pthread 条件变量?
【发布时间】:2014-01-13 09:07:15
【问题描述】:

pthread问题:

似乎只有在其他线程调用 pthread_cond_notify 之前调用 pthread_cond_wait 时,条件变量才有效。 如果在等待之前以某种方式发生通知,则等待将被卡住。

我的问题是:什么时候应该使用条件变量?

调度程序可以抢占线程,并且在等待之前可能会发生通知。

等待信号量没有这个问题——它们有一个计数器。

什么时候条件变量比信号量好?

这是一个测试:

文件 condvar.c

#include <pthread.h>
#include <stdio.h>
#include <stdlib.h>

// test of conditional variables;
// if cond-var is notified before wait starts, then wait never wakes up !!!
// better to use semaphores than this crap.

pthread_mutex_t cond_var_lock =  PTHREAD_MUTEX_INITIALIZER; 
pthread_cond_t cond_var = PTHREAD_COND_INITIALIZER;

int wait_first = 1;

void *tfunc(void *arg)
{
    (void) arg;

    if (!wait_first)
        sleep(1);

    fprintf(stderr,"on enter cond_var_lock %lx\n", pthread_self());
    pthread_mutex_lock( &cond_var_lock);
    fprintf(stderr,"before pthread_cond_wait %lx\n", pthread_self());
    pthread_cond_wait( &cond_var, &cond_var_lock);
    fprintf(stderr,"after pthread_cond_wait %lx\n", pthread_self());
    pthread_mutex_unlock( &cond_var_lock);
    fprintf(stderr,"after exit cond_var_lock %lx\n", pthread_self());

    return 0;
}

int main(int argc, char *argv[])
{
    pthread_t th;

    if (argc > 0) 
    wait_first = atoi( argv[1] );

    if (wait_first)
    {
        fprintf(stderr,"********* Wait first ***********\n");
    } else {
        fprintf(stderr,"********* Notify first *********\n");
    }


    pthread_create( &th, 0, tfunc, 0 );

    if (wait_first)
    {
        sleep(1);
    } 

    fprintf(stderr, "! on enter cond_var_lock %lx\n", pthread_self());
    pthread_mutex_lock( &cond_var_lock);
    fprintf(stderr, "! before pthread_cond_signal %lx\n", pthread_self());
    pthread_cond_signal( &cond_var );
    fprintf(stderr, "! after pthread_cond_signal %lx\n", pthread_self());
    pthread_mutex_unlock( &cond_var_lock);
    fprintf(stderr, "! after exit cond_var_lock %lx\n", pthread_self());

    sleep(5);
    return 0;    
}

文件 test.sh

#!/bin/sh

set -e
set -x

gcc condvar.c -o condvar -lpthread

./condvar 1

./condvar 0

测试输出

Output:

+ gcc condvar.c -o condvar -lpthread
+ ./condvar 1
********* Wait first ***********
on enter cond_var_lock b7779b70
before pthread_cond_wait b7779b70
! on enter cond_var_lock b777a6c0
! before pthread_cond_signal b777a6c0
! after pthread_cond_signal b777a6c0
! after exit cond_var_lock b777a6c0
after pthread_cond_wait b7779b70
after exit cond_var_lock b7779b70
+ ./condvar 0
********* Notify first *********
! on enter cond_var_lock b785c6c0
! before pthread_cond_signal b785c6c0
! after pthread_cond_signal b785c6c0
! after exit cond_var_lock b785c6c0
on enter cond_var_lock b785bb70
before pthread_cond_wait b785bb70

【问题讨论】:

标签: c linux pthreads


【解决方案1】:

条件变量应该用作等待和被通知的地方。它们不是条件本身,也不是事件。条件包含在周围的编程逻辑中。条件变量的典型使用模式是

// safely examine the condition, prevent other threads from
// altering it
pthread_mutex_lock (&lock);
while ( SOME-CONDITION is false)
    pthread_cond_wait (&cond, &lock);

// Do whatever you need to do when condition becomes true
do_stuff();
pthread_mutex_unlock (&lock);

另一方面,一个线程,发出条件变量的信号, 通常看起来像

// ensure we have exclusive access to whathever comprises the condition
pthread_mutex_lock (&lock);

ALTER-CONDITION

// Wakeup at least one of the threads that are waiting on the condition (if any)
pthread_cond_signal (&cond);

// allow others to proceed
pthread_mutex_unlock (&lock)

【讨论】:

  • 如何解决以下问题?如果在pthread_cond_wait之前调用了pthread_cond_notify,那么wait会被卡住;
  • 如果你调用pthread_cond_wait,这意味着条件为假。同时它不能改变,因为它受到互斥锁的保护。条件中的阻塞和互斥锁的相应解锁是原子的。
  • 在第一个block中,也可以调用do_stuff();在 pthread_mutex_unlock (&​​lock); 之后
  • @chill 多年后...我正在尝试自己理解条件变量。我认为“while(SOME-CONDITION is false)”应该是“if(SOME-CONDITION is false)”,因为条件变量被精确地用于避免while循环等待。我错了吗?
  • @Sam,可以从pthread_cond_wait 调用返回,条件仍然(或再次!)为假。因此它应该再次测试,因此是循环。没关系,它仍然不是一个忙等待轮询循环。 :)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2019-04-05
  • 2011-02-11
  • 1970-01-01
  • 2021-08-24
  • 2015-11-04
  • 2018-10-08
相关资源
最近更新 更多