Linux多线程4-3_条件变量

一、为什么要使用条件变量

一个典型的实例:
在一条生产先线上有一个仓库,当生产者生产的时候需要锁住仓库独占,而消费者取产品的时候也要锁住仓库独占。如果生产者发现仓库满了,那么他就不能生产了,变成了阻塞状态。但是此时由于生产者独占仓库,消费者又无法进入仓库去消耗产品,这样就造成了一个僵死状态。

我们需要一种机制,当互斥量被锁住以后发现当前线程还是无法完成自己的操作,那么它应该释放互斥量,让其他线程工作。
​ 1、可以采用轮询的方式,不停的查询你需要的条件
​ 2、让系统来帮你查询条件,使用条件变量pthread_cond_t cond

二、条件变量初始化与销毁

条件变量使用之前需要初始化:
1、pthread_cond_t cond = PTHREAD_COND_INITIALIZER;
2、int pthread_cond_init(pthread_cond_t *restrict cond, const pthread_condattr_t *restrict attr);
默认属性为空NULL

​ 条件变量使用完成之后需要销毁
int pthread_cond_destroy(pthread_cond_t *cond);

三、如何使用条件变量

条件变量使用需要配合互斥量
int pthread_cond_wait(pthread_cond_t *restrict cond, pthread_mutex_t *restrict mutex);

1、使用pthread_cond_wait等待条件变为真。传递给pthread_cond_wait的互斥量对条件进行保护,调用者把锁住的互斥量传递给函数。
2、这个函数将线程放到等待条件的线程列表上,然后对互斥量进行解锁,这是个原子操作。当条件满足时这个函数返回,返回以后继续对互斥量加锁。

int pthread_cond_timedwait(pthread_cond_t *restrict cond, pthread_mutex_t *restrict mutex, const struct timespec *restrict abstime);

3、这个函数与pthread_cond_wait类似,只是多一个timeout,如果到了指定的时间条件还不满足,那么就返回。时间用下面的结构体表示
struct timespec{ time_t tv_sec; long tv_nsec; };
注意,这个时间是绝对时间。例如你要等待3分钟,就要把当前时间加上3分钟然后转换到 timespec,而不是直接将3分钟转换到 timespec。

当条件满足的时候,需要唤醒等待条件的线程
int pthread_cond_broadcast(pthread_cond_t *cond);
int pthread_cond_signal(pthread_cond_t *cond);
​ 1、pthread_cond_broadcast唤醒等待条件的所有线程
​ 2、pthread_cond_signal至少唤醒等待条件的某一个线程
​ 注意,一定要在条件改变以后在唤醒线程

四、实例,生产者与消费者问题

1、程序框架

2、源代码

#include <stdio.h>
#include <stdlib.h>
#include <windows.h>
#include <iostream>
#include <queue>


#include "include/pthread.h"



#ifndef _WIN64
#pragma comment(lib,".\\lib32\\pthreadVC2.lib")
#else
#pragma comment(lib,".\\lib64\\pthreadVC2.lib")
#endif 

/*
在一条生产先线上有一个仓库,当生产者生产的时候需要锁住仓库独占,而消费者取产品的时候也要锁住仓库独占。
如果生产者发现仓库满了,那么他就不能生产了,变成了阻塞状态。但是此时由于生产者独占仓库,消费者又无法进
入仓库去消耗产品,这样就造成了一个僵死状态。

​ 我们需要一种机制,当互斥量被锁住以后发现当前线程还是无法完成自己的操作,那么它应该释放互斥量,让其他线程工作。
​ 1、可以采用轮询的方式,不停的查询你需要的条件
​ 2、让系统来帮你查询条件,使用条件变量pthread_cond_t cond

条件变量使用之前需要初始化:
1、pthread_cond_t cond = PTHREAD_COND_INITIALIZER;
2、int pthread_cond_init(pthread_cond_t *restrict cond, const pthread_condattr_t *restrict attr);
默认属性为空NULL
​ 条件变量使用完成之后需要销毁
​ int pthread_cond_destroy(pthread_cond_t *cond);

int pthread_cond_wait(pthread_cond_t *restrict cond, pthread_mutex_t *restrict mutex);
1、使用pthread_cond_wait等待条件变为真。传递给pthread_cond_wait的互斥量对条件进行保护,调用者把锁住的互斥量传递给函数。
2、这个函数将线程放到等待条件的线程列表上,然后对互斥量进行解锁,这是个原子操作。当条件满足时这个函数返回,返回以后继续对互斥量加锁。

当条件满足的时候,需要唤醒等待条件的线程
​ int pthread_cond_broadcast(pthread_cond_t *cond);
​ int pthread_cond_signal(pthread_cond_t *cond);
​ 1、pthread_cond_broadcast唤醒等待条件的所有线程
​ 2、pthread_cond_signal至少唤醒等待条件的某一个线程
​ 注意,一定要在条件改变以后在唤醒线程
*/



/*
调用pthread_cond_signal后要立刻释放互斥锁,因为pthread_cond_wait的最后一步是要将指定的互斥量重新锁住,如果
pthread_cond_signal之后没有释放互斥锁,pthread_cond_wait仍然要阻塞。
*/

#define BUFFER_SIZE 5 //产品库存大小 
#define PRODUCT_CNT 50 //产品生产总数 

struct product_cons
{
	int buffer[BUFFER_SIZE]; //生产产品值 
	pthread_mutex_t lock; //互斥锁 volatile int 
	int readpos, writepos; //读写位置 
	pthread_cond_t notempty; //条件变量,非空 
	pthread_cond_t notfull; //条件变量,非满 
}buffer;


//初始化
void init(struct product_cons *p)
{
	pthread_mutex_init(&p->lock, NULL); //互斥锁 
	pthread_cond_init(&p->notempty, NULL); //条件变量 
	pthread_cond_init(&p->notfull, NULL); //条件变量 
	p->readpos = 0; //读写位置 
	p->writepos = 0;
}

//销毁锁
void finish(struct product_cons *p)
{
	pthread_mutex_destroy(&p->lock); //互斥锁 
	pthread_cond_destroy(&p->notempty); //条件变量 
	pthread_cond_destroy(&p->notfull); //条件变量 
	p->readpos = 0; //读写位置 
	p->writepos = 0;
}

//存储 一个数据 到 bufferr 
void put(struct product_cons *p, int data) //输入产品子函数 
{
	pthread_mutex_lock(&p->lock);
	if ((p->writepos + 1) % BUFFER_SIZE == p->readpos)
	{
		printf("producer wait for not full\n");
		pthread_cond_wait(&p->notfull, &p->lock);
	}

	p->buffer[p->writepos] = data;
	p->writepos++;

	if (p->writepos >= BUFFER_SIZE)
		p->writepos = 0;

	pthread_cond_signal(&p->notempty);
	pthread_mutex_unlock(&p->lock);
}


//读,移除 一个数据 从 buffer 
int get(struct product_cons *p)
{
	int data;

	pthread_mutex_lock(&p->lock);

	if (p->readpos == p->writepos)
	{
		printf("consumer wait for not empty\n");
		pthread_cond_wait(&p->notempty, &p->lock);
	}

	data = p->buffer[p->readpos];
	p->readpos++;

	if (p->readpos >= BUFFER_SIZE)
		p->readpos = 0;

	pthread_cond_signal(&p->notfull);

	pthread_mutex_unlock(&p->lock);

	return data;
}

void *producer(void *data) //子线程 ,生产
{
	int n;
	for (n = 1; n <= 50; ++n) //生产 50 个产品 
	{
		Sleep(1);
		printf("put the %d product ...\n", n);
		put(&buffer, n);
		printf("put the %d product success\n", n);
	}

	printf("producer stopped\n");

	return NULL;
}

void *consumer(void *data)
{
	static int cnt = 0;
	int num;
	while (1)
	{
		Sleep(2);
		printf("get product ...\n");
		num = get(&buffer);
		printf("get the %d product success\n", num);
		if (++cnt == PRODUCT_CNT)
			break;
	}

	printf("consumer stopped\n");
	return NULL;
}

int main(int argc, char *argv[])
{
	pthread_t th_a, th_b;
	void *retval;

	init(&buffer);

	pthread_create(&th_a, NULL, producer, 0);
	pthread_create(&th_b, NULL, consumer, 0);

	pthread_join(th_a, &retval);
	pthread_join(th_b, &retval);

	finish(&buffer);

	return 0;
}

3、作业:多个生产者与多个消费者

/*多个生产者和多个消费者*/

#include <stdio.h>
#include <stdlib.h>
#include <windows.h>
#include <iostream>
#include <queue>


#include "include/pthread.h"



#ifndef _WIN64
#pragma comment(lib,".\\lib32\\pthreadVC2.lib")
#else
#pragma comment(lib,".\\lib64\\pthreadVC2.lib")
#endif 



#define BUFFER_SIZE 5 //产品库存大小
#define PRODUCT_CNT 50 //产品生产总数 

struct product_cons
{
    int buffer[BUFFER_SIZE]; //生产产品值
    pthread_mutex_t lock; //互斥锁 volatile int
    int readpos, writepos; //读写位置
    pthread_cond_t notempty; //条件变量,非空
    pthread_cond_t notfull; //非满

    pthread_mutex_t lock2; //互斥锁,控制cnt_p
    int cnt_p; //完成生产产品数量

    pthread_mutex_t lock3; //互斥锁,控制cnt_c
    int cnt_c; //获得生产产品数量
}buffer;

void init(struct product_cons *p)
{
    pthread_mutex_init(&p->lock, NULL); //互斥锁
    pthread_cond_init(&p->notempty, NULL); //条件变量
    pthread_cond_init(&p->notfull, NULL); //条件变量
    p->readpos = 0; //读写位置
    p->writepos = 0;

    pthread_mutex_init(&p->lock2, NULL);
    p->cnt_p = 0;

    pthread_mutex_init(&p->lock3, NULL);
    p->cnt_c = 0;
}

void fini(struct product_cons *p)
{
    pthread_mutex_destroy(&p->lock); //互斥锁
    pthread_cond_destroy(&p->notempty); //条件变量
    pthread_cond_destroy(&p->notfull); //条件变量
    p->readpos = 0; //读写位置
    p->writepos = 0;

    pthread_mutex_destroy(&p->lock2); 
    p->cnt_p = 0;

    pthread_mutex_destroy(&p->lock3); 
    p->cnt_c = 0;
}

//存储 一个数据 到 bufferr
void put(struct product_cons *p, int data) //输入产品子函数
{
    pthread_mutex_lock(&p->lock); //上锁
        
    /*等待,直到 buffer 不为 满*/
    while((p->writepos + 1) % BUFFER_SIZE == p->readpos) //测试空间是否已满
    {
        printf("producer wait for not full\n");
        pthread_cond_wait(&p->notfull, &p->lock); //阻塞等待
        //这里,生产者 notfull 等待消费者 pthread_cond_signal(&p->notfull);信号
        //如果,消费者发送了 signal 信号,表示有了 空闲
    }

    p->buffer[p->writepos] = data; //写数据
    p->writepos++;

    if(p->writepos >= BUFFER_SIZE) //如果写到 尾部,返回
        p->writepos = 0;
        
    pthread_cond_signal(&p->notempty); //发送有数据信号
    pthread_mutex_unlock(&p->lock); //解锁 
}

//读,移除 一个数据 从 buffer
int get(struct product_cons *p)
{
    int data = 0;
    pthread_mutex_lock(&p->lock);

    /*等待,直到不为空*/
    while(p->writepos == p->readpos)
    {
        printf("consumer wait for not empty\n");
        pthread_cond_wait(&p->notempty,&p->lock);
    }

    /*读 一个 数据*/
    data = p->buffer[p->readpos]; 
    p->readpos++;

    if(p->readpos >= BUFFER_SIZE) //如果读到 尾
        p->readpos = 0;

    pthread_cond_signal(&p->notfull);
    pthread_mutex_unlock(&p->lock);

    return data; 
}

void *producer(void *data) //子线程 ,生产
{
    int flag = -1;
    while(1)
    {
        pthread_mutex_lock(&buffer.lock2);
        if(buffer.cnt_p < PRODUCT_CNT)
        {
            ++buffer.cnt_p;
            printf("%s put the %d product\n", (char*)data, buffer.cnt_p);
            put(&buffer, buffer.cnt_p); 
        }
        else
            flag = 0;
        pthread_mutex_unlock(&buffer.lock2);

        if(!flag)
            break;

        sleep(2);
    } 

    printf("%s producer stopped\n", (char*)data);
    return NULL;
}

void *consumer(void *data)
{
    int flag = -1;
    while(1)
    {
        pthread_mutex_lock(&buffer.lock3);
        if(buffer.cnt_c < PRODUCT_CNT)
        {
            printf("%s get the %d product\n", (char*)data, get(&buffer));
            ++buffer.cnt_c;
        }
        else
            flag = 0;
        pthread_mutex_unlock(&buffer.lock3);
        if(!flag)
            break;

        sleep(2);
    }
        
    printf("%s consumer stopped\n", (char*)data);
    return NULL;
}

int main(int argc, char *argv[])
{
    pthread_t th_a[3],th_b[3];
    void *retval;
        
    init(&buffer);
        
    pthread_create(&th_a[0], NULL, producer, (void*)"th_a[0]");
    pthread_create(&th_a[1], NULL, producer, (void*)"th_a[1]"); 
    pthread_create(&th_a[2], NULL, producer, (void*)"th_a[2]");
    pthread_create(&th_b[0], NULL, consumer, (void*)"th_b[0]");
    pthread_create(&th_b[1], NULL, consumer, (void*)"th_b[1]");
    pthread_create(&th_b[2], NULL, consumer, (void*)"th_b[2]");

    pthread_join(th_a[0], &retval);
    pthread_join(th_a[1], &retval);
    pthread_join(th_a[2], &retval);
    pthread_join(th_b[0], &retval);
    pthread_join(th_b[1], &retval);
    pthread_join(th_b[2], &retval);

    fini(&buffer);

    return 0;
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值