linux 条件变量函数,linux系统编程:线程同步-条件变量(cond)

线程同步-条件变量(cond)

生产者与消费者问题

再引入条件变量之前,我们先看下生产者和消费者问题:生产者不断地生产产品,同时消费者不断地在消费产品。

这个问题的同步在于两处:第一,消费者之间需要同步:同一件产品只可由一人消费。第二,当无产品可消费时,消费者需等待生产者生产后,才可继续消费,这又是一个同步问题。详细了解:生产者消费者问题。

条件变量

条件变量是利用线程间共享的全局变量进行同步的一种机制,并且条件变量总是和互斥锁结合在一起。

相关函数

pthread_cond_t //条件变量类型

pthread_cond_init

pthread_cond_destroy

pthread_cond_wait(pthread_cond_t *, pthread_mutex_t *)

pthread_cond_timedwait

pthread_cond_signal

pthread_cond_broadcast

其中,需要着重理解的是pthread_cond_wait()方法,它有三个作用:

所有运行到此处的线程被阻塞,直到条件变量的唤醒。

在阻塞的同时释放锁。

当被唤醒后,重新去获取锁。

唤醒线程的函数有两个:pthread_cond_signal和pthread_cond_broadcast,前者唤醒一个,后者唤醒所有的。

生产者与消费者示例代码

运用条件变量,我们来解决下生产者和消费者问题:

#include

#include

#include

#include

struct goods

{

int id;

struct goods *next;

};

pthread_mutex_t m;

pthread_cond_t has_product;

struct goods *head;

void *producer(void *argv)

{

struct goods *p = NULL;

while (1)

{

pthread_mutex_lock(&m);

p = malloc(sizeof(struct goods));

p->id = rand() % 100;

p->next = head;

head = p;

printf("produce %d\n", p->id);

pthread_mutex_unlock(&m);

pthread_cond_signal(&has_product);

//printf("produce %d\n", p->id);

sleep(rand() % 2);

}

return (void *)0;

}

void *comsumer(void *argv)

{

struct goods *p = NULL;

while (1)

{

pthread_mutex_lock(&m);

//思考:pthread_cond_wait()的作用?

while (NULL == head)

pthread_cond_wait(&has_product, &m);

p = head;

head = head->next;

printf("comsume %d\n", p->id);

pthread_mutex_unlock(&m);

//printf("comsume %d\n", p->id);

free(p);

sleep(rand() % 2);

}

return (void *)0;

}

int main(void)

{

int i;

//初始化条件变量和互斥量

pthread_mutex_init(&m, NULL);

pthread_cond_init(&has_product, NULL);

head = NULL;

pthread_t pro[2], com[3];

for (i = 0; i < 2; i++)

pthread_create(&pro[i], NULL, producer, NULL);

for (i = 0; i < 3; i++)

pthread_create(&com[i], NULL, comsumer, NULL);

for (i = 0; i < 2; i++)

pthread_join(pro[i], NULL);

for (i = 0; i < 3; i++)

pthread_join(com[i], NULL);

//销毁条件变量和互斥量

pthread_mutex_destroy(&m);

pthread_cond_destroy(&has_product);

return 0;

}

在代码中,我们开启两个线程作为生产者,三个线程作为消费者。产品使用链表存储,并且每次生产和消费都在链表头部发生。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值