条件变量,pthread_cond_wait()用法分析

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/paulkg12/article/details/84663378

前言

无论哪种等待方式,都必须和一个互斥锁配合,以防止多个线程同时请求pthread_cond_wait()(或pthread_cond_timedwait(),下同)的竞争条件(Race Condition)。mutex互斥锁必须是普通锁(PTHREAD_MUTEX_TIMED_NP)或者适应锁(PTHREAD_MUTEX_ADAPTIVE_NP),且在调用pthread_cond_wait()前必须由本线程加锁(pthread_mutex_lock()),而在更新条件等待队列以前,mutex保持锁定状态,并在线程挂起进入等待前解锁。在条件满足从而离开pthread_cond_wait()之前,mutex将被重新加锁,以与进入pthread_cond_wait()前的加锁动作对应

如果看不懂,请看原文,或者后记

动手实践

这里参考了:

https://blog.csdn.net/hairetz/article/details/4535920

代码

#include <pthread.h>
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
 
static pthread_mutex_t mtx = PTHREAD_MUTEX_INITIALIZER;
static pthread_cond_t cond = PTHREAD_COND_INITIALIZER;
 
struct node {
int n_number;
struct node *n_next;
} *head = NULL;
 
/*[thread_func]*/
static void cleanup_handler(void *arg)
{
    printf("Cleanup handler of second thread.\n");
    free(arg);
    (void)pthread_mutex_unlock(&mtx);
}
static void *thread_func(void *arg)
{
    struct node *p = NULL;
 
    pthread_cleanup_push(cleanup_handler, p);
    while (1) {
        pthread_mutex_lock(&mtx);           //这个mutex主要是用来保证pthread_cond_wait的并发性
        while (head == NULL)   {               //这个while要特别说明一下,单个pthread_cond_wait功能很完善,为何这里要有一个while (head == NULL)呢?因为pthread_cond_wait里的线程可能会被意外唤醒,如果这个时候head != NULL,则不是我们想要的情况。这个时候,应该让线程继续进入pthread_cond_wait
            pthread_cond_wait(&cond, &mtx);         // pthread_cond_wait会先解除之前的pthread_mutex_lock锁定的mtx,然后阻塞在等待对列里休眠,直到再次被唤醒(大多数情况下是等待的条件成立而被唤醒,唤醒后,该进程会先锁定先pthread_mutex_lock(&mtx);,再读取资源
            //用这个流程是比较清楚的/*block-->unlock-->wait() return-->lock*/
        }
        p = head;
        head = head->n_next;
        printf("Got %d from front of queue\n", p->n_number);
        free(p);
        pthread_mutex_unlock(&mtx);             //临界区数据操作完毕,释放互斥锁
    }
    pthread_cleanup_pop(0);// 0 代表清理函数在return 的时候 不会被调用,但是如果因为是cancel 掉的,所以会调用
    return 0;
}
 
int main(void)
{
    pthread_t tid;
    int i;
    struct node *p;
    pthread_create(&tid, NULL, thread_func, NULL);   //子线程会一直等待资源,类似生产者和消费者,但是这里的消费者可以是多个消费者,而不仅仅支持普通的单个消费者,这个模型虽然简单,但是很强大
    /*[tx6-main]*/
    for (i = 0; i < 10; i++) {
        p = malloc(sizeof(struct node));
        p->n_number = i;
        pthread_mutex_lock(&mtx);             //需要操作head这个临界资源,先加锁,
        p->n_next = head;
        head = p;
        pthread_cond_signal(&cond);
        pthread_mutex_unlock(&mtx);           //解锁
        sleep(1);
    }
    printf("thread 1 wanna end the line.So cancel thread 2.\n");
    pthread_cancel(tid);             //关于pthread_cancel,有一点额外的说明,它是从外部终止子线程,子线程会在最近的取消点,退出线程,而在我们的代码里,最近的取消点肯定就是pthread_cond_wait()了。关于取消点的信息,有兴趣可以google,这里不多说了
    pthread_join(tid, NULL);
    printf("All done -- exiting\n");
    return 0;
}

效果

在这里插入图片描述

另个动手的实践(生产者,消费者模型)

参考了:

https://blog.csdn.net/u013796074/article/details/54099894?locationNum=10&fps=1

//conditionVarTest.c
#include <pthread.h>
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <signal.h>

pthread_cond_t cond_product;
pthread_cond_t cond_consume;

pthread_mutex_t lock;

void init_work(void) 
{
	//条件变量
	pthread_cond_init(&cond_product, NULL);
	pthread_cond_init(&cond_consume, NULL);
	// 线程锁
	pthread_mutex_init(&lock, NULL);
}

void *handle_product(void *arg) {
	int i;
	int *product = NULL;
	product = (int *) arg;
	for (i = 1; i < 50; i++) {
		pthread_mutex_lock(&lock);
		if (*product >= 4) {
			printf("\033[43mRepo满了, stop produce...\033[0m\n");
			pthread_cond_wait(&cond_product, &lock);
		}
		printf("producing...\n");
		sleep(2);
		printf("%s\n", "success produce");
		*product+=1;
		pthread_cond_signal(&cond_consume);// notify the consumers that we have some thing to sell 
		printf("\033[32m produce a product, 生产了:%d 次,仓库里剩余%d 个 \033[0m\n", i, *product);
		printf("发送信号---> 生产成功!\n");
		pthread_mutex_unlock(&lock);
		usleep(50000);
	}
	return NULL;
}

void *handle_consume(void *arg) {
	int i;
	int *product = NULL;
	product = (int *) arg;
	for(i = 1; i < 50; i++) {
		pthread_mutex_lock(&lock);
		if (*product <= 1) {
			printf("\033[43m缺货了,请等待。。。\033[0m\n");
			pthread_cond_wait(&cond_consume, &lock);
		}
		// eat the product
		printf("%s\n", "消费中...");
		sleep(2);
		*product-=2;
		printf("%s\n", "消费完成");
		printf("\033[31m消费两个产品,共消费%d次,仓库剩余%d个\033[0m\n",i,*product);
		pthread_cond_signal(&cond_product);
		printf("%s\n", "发信号--->已经消费了");
		pthread_mutex_unlock(&lock);
		usleep(30000);
		if ( i%6 == 0) {
			sleep(9);
		}
	}
	return NULL;
}

int main() {
	pthread_t th_product, th_consume;
	int ret;
	int intrinsic = 3;//这个是生产者消费者共享的变量啊。原来
	init_work();
	ret = pthread_create(&th_product, 0, handle_product, &intrinsic);
	if (ret != 0) {
		perror("创建生产线程失败\n");
		exit(1);
	}

	ret = pthread_create(&th_consume, 0, handle_consume, &intrinsic);
	if (ret != 0) {
		perror("创建消费线程失败\n");
		exit(1);
	}

	pthread_join(th_product, 0);//回收生产线程
	pthread_join(th_consume, 0);//回收消费线程

	return 0;
}

效果:

在这里插入图片描述

要搞清楚 为什么需要条件变量,请移步美国文章: http://pages.cs.wisc.edu/~remzi/OSTEP/threads-cv.pdf

第三份参考

https://blog.csdn.net/u013106951/article/details/52194053

我从这个参考里,领悟到的是,使用条件变量,你要明白前言所说的那段话。

APUE里是这样说的:(还不是很理解1130 2018)

在这里插入图片描述

我自己说的语言是:你可以在两个线程同时上锁(这与备注里的重复上锁是不一样的哦, 不会导致死锁),然后在锁范围内进行条件变量的check。这就是如何使用条件变量的完整叙述。


一个线程先上锁,然后等

在这里插入图片描述

另外一个也上锁,然后做完自己的事情后,进行发信号。

在这里插入图片描述

上面那个在等的线程,接收到信号,就会在pthread_cond_wait()里尝试 1. unlock mutex 2. lock mutex

你总是应该这样设计:

在两个线程同时上锁,然后在锁范围内进行条件变量的check

  • 备注:

不可以在同一个线程重复上锁
在这里插入图片描述

后记:学习OS的好去处

Operating Systems: Three Easy Pieces:

http://pages.cs.wisc.edu/~remzi/OSTEP/

猜你喜欢

转载自blog.csdn.net/paulkg12/article/details/84663378