linux编程---线程---条件变量

条件变量通信机制

基本原理

初始化条件变量
int pthread_cond_init(pthread_cond_t *restrict cond,
              const pthread_condattr_t *restrict attr);
              
pthread_cond_t cond = PTHREAD_COND_INITIALIZER;

销毁条件变量
int pthread_cond_destroy(pthread_cond_t *cond);

通知等待条件变量的线程
用于唤醒等待出现与条件变量cond关联的条件的所有线程
int pthread_cond_broadcast(pthread_cond_t *cond);
用于唤醒等待出现与条件变量cond关联的条件的第一条线程
int pthread_cond_signal(pthread_cond_t *cond);

等待条件变量
int pthread_cond_timedwait(pthread_cond_t *restrict cond,
              pthread_mutex_t *restrict mutex,
              const struct timespec *restrict abstime);
              
int pthread_cond_wait(pthread_cond_t *restrict cond,
              pthread_mutex_t *restrict mutex);
如果某线程因等待条件变量进入等待状态时,将隐含释放其申请的互斥锁。
同样,在返回时,首先要申请到该互斥锁对象。
              
程序示例:
程序处理生产消费问题,整个临时存储空间为2,即在任意时刻,最多能够有2个产品存放在临时
空间,如果已经有2个产品存放在临时空间,将阻塞生产线程。如果临时空间中没有产品,显示
需要阻塞消费线程。程序主要实现了生产和消费两个线程同步。

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

#define BUFSIZE 2

struct prodcons
{
    int buf[BUFSIZE];
    pthread_mutex_t lock;
    int readpos,writepos;
    pthread_cond_t notempty;
    pthread_cond_t notfull;
};

void init(struct prodcons *prod)
{
    pthread_mutex_init(&prod->lock,NULL);
    pthread_cond_init(&prod->notempty,NULL);
    pthread_cond_init(&prod->notfull,NULL);
    prod->readpos = 0;
    prod->writepos = 0;
}

void put(struct prodcons* prod,int data)
{
    pthread_mutex_lock(&prod->lock);
    while((prod->writepos+1) % BUFSIZE == prod->readpos)
    {
        printf("producer wait for not full\n");
        pthread_cond_wait(&prod->notfull,&prod->lock);
    }
    prod->buf[prod->writepos] = data;
    prod->writepos++;
    if(prod->writepos >= BUFSIZE)
            prod->writepos = 0;
    pthread_cond_signal(&prod->notempty);
    pthread_mutex_unlock(&prod->lock);
}

int get(struct prodcons* prod)
{
    int data;
    pthread_mutex_lock(&prod->lock);
    while(prod->writepos == prod->readpos)
    {
        printf("consumer wait for not empty\n");
        pthread_cond_wait(&prod->notempty,&prod->lock);
    }
    data = prod->buf[prod->readpos];
    prod->readpos++;
    if(prod->readpos >= BUFSIZE)
            prod->readpos = 0;
    pthread_cond_signal(&prod->notfull);
    pthread_mutex_unlock(&prod->lock);
    return data;
}

#define OVER (-1)
struct prodcons buf;

void* producer(void* data)
{
    int n;
    for(n=1;n <= 5;n++)
    {
        printf("producer sleep 1 second....\n");
        sleep(1);
        printf("put the %d producet\n",n);
        put(&buf,n);
    }
    for(n=6;n <= 10;n++)
    {
        printf("producer sleep 3 second....\n");
        sleep(3);
        printf("put the %d producet\n",n);
        put(&buf,n);
    }
    put(&buf,OVER);
    printf("producer stopped\n");
    return NULL;
}

void* consumer(void* data)
{
    int d =0;
    while(1)
    {
        printf("consumer sleep 2 second...\n");
        sleep(2);
        d = get(&buf);
        printf("get the %d product\n",d);
        if(d == OVER)
            break;
    }
    printf("consumer stopped\n");
    return NULL;
}

int main()
{
    pthread_t th1,th2;
    void* retval;
    init(&buf);
    pthread_create(&th1,NULL,producer,0);
    pthread_create(&th2,NULL,consumer,0);
    
    pthread_join(th1,&retval);
    pthread_join(th2,&retval);

    return 0;
}

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值