您的位置:首页 > 运维架构 > Linux

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

2017-08-23 15:00 239 查看
条件变量通信机制

基本原理

初始化条件变量

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;

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: