Linux之线程条件变量cond

概念:条件变量不是锁,要和互斥量组合使用。条件变量就是生产者“生产”完成,消费者才能“使用”,如果没有“产品”,消费者就会被条件变量cond阻塞等待生产者“生产”。(生产者与消费者模型)

函数:

  • 1.超时等待
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);

struct timespec{

           time_t tv_sec;  /*seconds*/秒

           long tv_nsec;  /*nanoseconds*/纳秒

}

tv_sec 绝对时间,填写的时候,例如time(NULL)+600,表示设置超时600s。

  • 2.条件变量阻塞等待
       int pthread_cond_wait(pthread_cond_t *restrict cond,
           pthread_mutex_t *restrict mutex);

先释放锁mutex;

阻塞在cond条件变量上。

  • 3.初始化一个条件变量
       int pthread_cond_init(pthread_cond_t *restrict cond,
           const pthread_condattr_t *restrict attr);
       pthread_cond_t cond = PTHREAD_COND_INITIALIZER;
  • 4.销毁一个条件变量
int pthread_cond_destroy(pthread_cond_t *cond);
  • 5.信号,唤醒至少一个阻塞在条件变量cond上的线程
int pthread_cond_signal(pthread_cond_t *cond);
  • 6.唤醒阻塞在条件变量cond上的全部线程
int pthread_cond_broadcast(pthread_cond_t *cond);

示例:创建两个线程,一个线程为生产者,两个线程为消费者。(生产者与消费者模型)

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

int beginnum = 1000;

//初始化互斥锁和条件变量
pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
pthread_cond_t cond = PTHREAD_COND_INITIALIZER;

//定义一个链表,用于存取数据
typedef struct _ProdInfo
{
    int num;
    struct _ProdInfo *next;
}ProdInfo;

ProdInfo *Head = NULL;

void *thr_producter(void *arg)
{
    //负责在链表中添加数据
    while (1)
    {
        ProdInfo * prod = malloc(sizeof(ProdInfo));
        prod->num = beginnum++;
        printf("----%s----self = %lu----%d\n",__FUNCTION__,pthread_self(),prod->num);
        pthread_mutex_lock(&mutex);
        //添加到链表
        prod->next = Head;
        Head = prod;
        pthread_mutex_unlock(&mutex);
        //发起通知
        pthread_cond_signal(&cond);

        sleep(rand()%2);
        //free(prod);  //重复释放内存会报错,用完在释放
    }
    
    return NULL;
}

void *thr_customer(void *arg)
{
    ProdInfo *prod = NULL;

    while (1)
    {
        //获取链表中的数据
        pthread_mutex_lock(&mutex);
        //if(Head = NULL)
        while (Head == NULL)
        {
            pthread_cond_wait(&cond,&mutex);  //在此之前必须加锁
        }

        prod = Head;
        Head = Head->next;
        printf("----%s----self = %lu----%d\n",__FUNCTION__,pthread_self(),prod->num);
        pthread_mutex_unlock(&mutex);
        sleep(rand()%4);
        free(prod);
    }
    
    return NULL;
}

int main()
{
    pthread_t tid[3];
    pthread_create(&tid[0],NULL,thr_producter,NULL);
    pthread_create(&tid[1],NULL,thr_customer,NULL);
    pthread_create(&tid[2],NULL,thr_customer,NULL);

    pthread_join(tid[0],NULL);
    pthread_join(tid[1],NULL);
    pthread_join(tid[2],NULL);
    pthread_mutex_destroy(&mutex);
    pthread_mutex_destroy(&cond);

    return 0;
}

输出:

~$ gcc cond_product.c -lpthread

~$ ./a.out
----thr_producter----self = 140197865371392----1000
----thr_customer----self = 140197856978688----1000
----thr_producter----self = 140197865371392----1001
----thr_customer----self = 140197731759872----1001
----thr_producter----self = 140197865371392----1002
----thr_customer----self = 140197856978688----1002
----thr_producter----self = 140197865371392----1003
----thr_producter----self = 140197865371392----1004
----thr_producter----self = 140197865371392----1005
----thr_customer----self = 140197731759872----1005
----thr_producter----self = 140197865371392----1006
----thr_producter----self = 140197865371392----1007
----thr_customer----self = 140197856978688----1007
----thr_customer----self = 140197731759872----1006

 

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
条件变量是一种在多线程环境下进行同步和通信的机制。在 Linux 操作系统中,条件变量是通过 pthread 库提供的。 pthread 库中定义了 pthread_cond_t 类型,表示条件变量条件变量需要与互斥锁一起使用,以保证线程间的同步和互斥访问。 条件变量的基本操作包括: 1. pthread_cond_init:初始化条件变量。 2. pthread_cond_wait:线程等待条件变量满足。 3. pthread_cond_signal:唤醒等待条件变量的一个线程。 4. pthread_cond_broadcast:唤醒等待条件变量的所有线程。 5. pthread_cond_destroy:销毁条件变量。 使用条件变量时,一般的模式是在等待条件变量线程中加锁,然后等待条件变量满足,在满足条件后解锁。唤醒线程时,先加锁,再改变条件变量的状态,然后解锁。 例如,一个生产者-消费者问题可以使用条件变量来实现。生产者和消费者共享一个缓冲区,生产者负责向缓冲区中写入数据,消费者负责从缓冲区中读取数据。当缓冲区为空时,消费者需要等待生产者向缓冲区中写入数据;当缓冲区满时,生产者需要等待消费者从缓冲区中读取数据。 在这种情况下,可以使用一个互斥锁来保护缓冲区的访问,使用两个条件变量来表示缓冲区的状态,一个表示缓冲区是否为空,一个表示缓冲区是否已满。生产者在向缓冲区写入数据时,如果缓冲区已满,则等待“缓冲区不满”条件变量;消费者在从缓冲区读取数据时,如果缓冲区为空,则等待“缓冲区不空”条件变量

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值