c语言线程唤醒机制——pthread_cond_wait

一、pthread_cond_wait()唤醒函数讲解
1.函数原型讲解
头文件

#include <pthread.h>

函数

int   pthread_cond_wait(pthread_cond_t   *cond,   pthread_mutex_t   *mutex);

函数功能:实现线程的睡眠
参数1:条件锁
参数2:互斥锁

二、程序demon

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

pthread_t atter;
pthread_mutex_t mutex=PTHREAD_MUTEX_INITIALIZER;
pthread_cond_t function1_cond=PTHREAD_COND_INITIALIZER;

int function_run=0;

void *function1()
{
	pthread_mutex_lock(&mutex);
	printf("======= this is function  1 =====\n");
	while(1){
		printf("======= this is function  2 =====\n");
		while(!function_run) 
		{
			printf("======= this is function  3 ===\n");
			pthread_cond_wait(&function1_cond,&mutex);
		}
		printf("======= this is function  4=====\n");
		function_run=0;
		pthread_mutex_unlock(&mutex);
	}
}

int create_pthread()
{ 
	if (-1 == pthread_create(&atter, NULL, function1, NULL)) {
		printf("Create pthread failed");
		return -1;
	}
	pthread_detach(atter);
	return 0;
}

int main()
{
	create_pthread();
	sleep(2);

	printf("\n第一次唤醒线程\n");
	pthread_mutex_lock(&mutex);
	function_run=1;
	pthread_cond_signal(&function1_cond);
	pthread_mutex_unlock(&mutex);

	sleep(2);
	printf("\n第二次唤醒线程\n");
	pthread_mutex_lock(&mutex);
	function_run=1;
	pthread_cond_signal(&function1_cond);
	pthread_mutex_unlock(&mutex);

	sleep(2);
	printf("\n第三次唤醒线程\n");
	pthread_mutex_lock(&mutex);
	function_run=1;
	pthread_cond_signal(&function1_cond);
	pthread_mutex_unlock(&mutex);

	sleep(2);
	printf("\n第四次唤醒线程\n");
	pthread_mutex_lock(&mutex);
	function_run=1;
	pthread_cond_signal(&function1_cond);
	pthread_mutex_unlock(&mutex);
	
	sleep(2);	
	pthread_detach(atter);
	return 0;
}

运行结果:
在这里插入图片描述

下面是一个示例的C语言代码,使用pthread_cond_signal和pthread_cond_wait实现交替打印0、基数和偶数的功能: ```c #include <stdio.h> #include <pthread.h> pthread_mutex_t mutex; pthread_cond_t cond; int count = 0; void* print_number(void* arg) { int num_type = *(int*)arg; while (count <= 7) { pthread_mutex_lock(&mutex); // 打印0 if (num_type == 0 && count == 0) { printf("0"); count++; pthread_cond_signal(&cond); } // 打印偶数 else if (num_type == 1 && count % 2 == 0) { printf("%d", count); count++; pthread_cond_signal(&cond); } // 打印奇数 else if (num_type == 2 && count % 2 != 0) { printf("%d", count); count++; pthread_cond_signal(&cond); } else { pthread_cond_wait(&cond, &mutex); } pthread_mutex_unlock(&mutex); } return NULL; } int main() { pthread_t threads[3]; int thread_args[3] = {0, 1, 2}; pthread_mutex_init(&mutex, NULL); pthread_cond_init(&cond, NULL); for (int i = 0; i < 3; i++) { pthread_create(&threads[i], NULL, print_number, &thread_args[i]); } for (int i = 0; i < 3; i++) { pthread_join(threads[i], NULL); } pthread_mutex_destroy(&mutex); pthread_cond_destroy(&cond); printf("\n"); return 0; } ``` 在这个示例中,我们使用了一个互斥锁mutex和一个条件变量cond来实现线程间的同步。三个线程分别代表打印0、偶数和奇数的功能。在每个线程中,我们使用了互斥锁来保护共享变量count,并使用条件变量来进行线程的阻塞和唤醒。通过调用pthread_cond_wait和pthread_cond_signal来实现线程间的交替打印。 注意,在主线程中,我们使用pthread_join来等待所有子线程的结束,以确保打印完所有数字之后再打印换行符。
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值