Linux 同步机制:条件变量

2017-01-13 08:23:06来源:CSDN作者:thisinnocence人点击

条件变量的优势

条件变量提供了一种线程间的通知机制,达到条件唤醒对应线程,配合互斥量,可以解决多线程中大多数的同步问题。需要信号量的解决问题的基本都可以用条件变量加互斥量解决。由于信号量使用起来容易出错,实际工程中用互斥量和条件变量的更多。

互斥量可以保护共享数据的原子访问,但是无法很好的保证条件时序,单纯的配合sleep调用的忙等待会浪费CPU,也无法做到第一时间感知到条件ok。这里配合条件变量就可以解决此场景了。一个典型的消费者线程先启动的场景的时序:

  1. 消费者线程启动,获取互斥量读缓冲区,发现为空,阻塞自己并同时释放互斥量;
  2. 生产者线程启动,获取互斥量写缓冲区,写完毕后,通知消费者同时释放互斥量;
  3. 消费者线程唤醒,获取互斥量读缓冲区;

条件变量常用API

这些API位于pthread.h中:

/* Initialize condition variable COND using attributes ATTR, or use   the default values if later is NULL.  */extern int pthread_cond_init (pthread_cond_t *__restrict __cond,                  const pthread_condattr_t *__restrict __cond_attr)     __THROW __nonnull ((1));/* Destroy condition variable COND.  */extern int pthread_cond_destroy (pthread_cond_t *__cond)     __THROW __nonnull ((1));/* Wake up one thread waiting for condition variable COND.  */extern int pthread_cond_signal (pthread_cond_t *__cond)     __THROWNL __nonnull ((1));/* Wake up all threads waiting for condition variables COND.  */extern int pthread_cond_broadcast (pthread_cond_t *__cond)     __THROWNL __nonnull ((1));/* Wait for condition variable COND to be signaled or broadcast.   MUTEX is assumed to be locked before.   This function is a cancellation point and therefore not marked with   __THROW.  */extern int pthread_cond_wait (pthread_cond_t *__restrict __cond,                  pthread_mutex_t *__restrict __mutex)     __nonnull ((1, 2));/* Initialize condition variable attribute ATTR.  */extern int pthread_condattr_init (pthread_condattr_t *__attr)     __THROW __nonnull ((1));/* Destroy condition variable attribute ATTR.  */extern int pthread_condattr_destroy (pthread_condattr_t *__attr)     __THROW __nonnull ((1));

举例

一个例子:1个生产者,2个消费者。生产者往缓冲区写入东西后,消费者才可以去缓冲区读。三个线程在SMP系统调度上是无法保证时序的,这是就要用到了具有通知机制的条件变量来实现同步。

只使用互斥量是不够的,消费者拿到锁,去读缓冲区,发现为空,然后阻塞自己同时释放锁。那么,作为生产者,拿到锁,写入缓冲区数据后,还要去通知消费者,同时释放锁。这样就比较完善的解决了生产者消费者的问题,并保证了时序。gcc main.c -lpthread编译记得链接pthread.

#include <pthread.h>#include <stdio.h>#include <string.h>char buff[100];int buff_len;pthread_mutex_t mutex;pthread_cond_t cond;void* th_writer(void *p){    pthread_mutex_lock(&mutex);    strcpy(buff, "hello, world!");    buff_len = strlen(buff);    printf("func[%s]: %s/n", __FUNCTION__, buff);    sleep(1);    // pthread_cond_signal(&cond); // signal 只能唤醒一个消费者    pthread_cond_broadcast(&cond); // 通知所有消费者    pthread_mutex_unlock(&mutex);  // 释放缓冲区,共享的数据区}void* th_reader1(void *p){    pthread_mutex_lock(&mutex);    if (buff_len == 0) {        printf("func[%s]: buff empty, block/n", __FUNCTION__);        pthread_cond_wait(&cond, &mutex); // 原子调用阻塞并解锁其互斥量    }    printf("func[%s]: %s/n", __FUNCTION__, buff);    sleep(1);    pthread_mutex_unlock(&mutex);}void* th_reader2(void *p){    pthread_mutex_lock(&mutex);    if (buff_len == 0) {        pthread_cond_wait(&cond, &mutex); // 原子调用阻塞并解锁其互斥量        printf("func[%s]: buff empty, block/n", __FUNCTION__);    }    printf("func[%s]: %s/n", __FUNCTION__, buff);    sleep(1);    pthread_mutex_unlock(&mutex);}int main(){    pthread_t tid1, tid2, tid3;    void *ret1, *ret2, *ret3;    memset(buff, 0, sizeof(buff));    pthread_mutex_init(&mutex, NULL);    pthread_cond_init(&cond, NULL);    printf("start thread reader 1/n");    pthread_create(&tid2, NULL, th_reader1, NULL);    printf("wait 1s in main thread./n");    sleep(1);    printf("start thread reader 2/n");    pthread_create(&tid3, NULL, th_reader2, NULL);    printf("start thread writer /n");    pthread_create(&tid1, NULL, th_writer, NULL);    pthread_join(tid1, &ret1);    pthread_join(tid2, &ret2);    pthread_join(tid3, &ret3);    pthread_mutex_destroy(&mutex);    pthread_cond_destroy(&cond);    return 0;}/*  运行结果:start thread reader 1wait 1s in main thread.func[th_reader1]: buff empty, blockstart thread reader 2start thread writerfunc[th_writer]: hello, world!func[th_reader1]: hello, world!func[th_reader2]: buff empty, blockfunc[th_reader2]: hello, world!*/

最新文章

123

最新摄影

微信扫一扫

第七城市微信公众平台