条件量与互斥锁的简单使用实例

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

static pthread_mutex_t m_lock;
static pthread_cond_t m_worker_cond;
static pthread_t pthread_a;
static pthread_t pthread_b;

int i=1;
void *thread1(void *junk)
{
	for(i=1;i<=6;i++)
	{
		pthread_mutex_lock(&m_lock);
		printf("thread1: lock %d\n", __LINE__);
		if(i%3==0){
			printf("thread1:signal 1 %d\n", __LINE__);
			pthread_cond_signal(&m_worker_cond);
			printf("thread1:signal 2 %d\n", __LINE__);
			sleep(1);
		}
		pthread_mutex_unlock(&m_lock);
		printf("thread1: unlock %d\n", __LINE__);
		sleep(1);
	}
}
void *thread2(void *junk)
{
	while(i<6)
	{
		pthread_mutex_lock(&m_lock);
		printf("thread2: lock %d\n", __LINE__);
		if(i%3!=0){
			printf("thread2: wait 1 %d\n", __LINE__);
			pthread_cond_wait(&m_worker_cond,&m_lock);
			printf("thread2: wait 2 %d\n", __LINE__);
		}
		pthread_mutex_unlock(&m_lock);
		printf("thread2: unlock %d\n", __LINE__);
		sleep(1);
	}
}
int main(int argc,char **argv)
{
	pthread_cond_init(&m_worker_cond, NULL);
	pthread_mutex_init(&m_lock, NULL);
	
	pthread_create(&pthread_a,NULL,thread1,(void *)NULL);
	pthread_create(&pthread_b,NULL,thread2,(void *)NULL); 

	pthread_join(pthread_a, NULL);
	pthread_join(pthread_b, NULL);

	pthread_mutex_destroy(&m_lock);
	pthread_cond_destroy(&m_worker_cond);
	return 0;
}

编译脚本 Makefile 如下:
make:
gcc -o pthread pthread.c -lpthread

clean:
rm -rf *.o

运行结果:
zhangtiangui@silead-fp:~/my_test_code$ ./pthread
thread1: lock 16
thread1: unlock 24
thread2: lock 33
thread2: wait 1 35
thread1: lock 16
thread1: unlock 24
thread1: lock 16
thread1:signal 1 18
thread1:signal 2 20
thread1: unlock 24
thread2: wait 2 37
thread2: unlock 40
thread1: lock 16
thread1: unlock 24
thread2: lock 33
thread2: wait 1 35
thread1: lock 16
thread1: unlock 24
thread1: lock 16
thread1:signal 1 18
thread1:signal 2 20
thread1: unlock 24
thread2: wait 2 37
thread2: unlock 40

详解:

  1. 主函数 main 创建并执行两个线程:
    pthread_create(&pthread_a,NULL,thread1,(void *)NULL);//线程pthread_a
    pthread_create(&pthread_b,NULL,thread2,(void *)NULL);//线程pthread_b

  2. 线程pthread_a操作函数thread1:
    void *thread1(void *junk)
    {
    for(i=1;i<=6;i++)
    {
    pthread_mutex_lock(&m_lock);
    printf(“thread1: lock %d\n”, LINE);
    if(i%3==0){
    printf(“thread1:signal 1 %d\n”, LINE);
    pthread_cond_signal(&m_worker_cond);
    printf(“thread1:signal 2 %d\n”, LINE);
    sleep(1);
    }
    pthread_mutex_unlock(&m_lock);
    printf(“thread1: unlock %d\n”, LINE);
    sleep(1);
    }
    }
    获得互斥锁,进锁/出锁,sleep(1);延时;

  3. 线程pthread_b操作函数thread2:
    void *thread2(void *junk)
    {
    while(i<6)
    {
    pthread_mutex_lock(&m_lock);
    printf(“thread2: lock %d\n”, LINE);
    if(i%3!=0){
    printf(“thread2: wait 1 %d\n”, LINE);
    pthread_cond_wait(&m_worker_cond,&m_lock);
    printf(“thread2: wait 2 %d\n”, LINE);
    }
    pthread_mutex_unlock(&m_lock);
    printf(“thread2: unlock %d\n”, LINE);
    sleep(1);
    }
    }
    获得互斥锁,进锁/出锁,
    pthread_cond_wait(&m_worker_cond,&m_lock);
    这是出锁,堵塞等待条件量变化后唤醒

  4. 唤醒:
    thread1函数中if(i%3==0)为真时,pthread_cond_signal(&m_worker_cond);唤醒等待的堵塞,注意这里必须先出锁后才能唤醒堵塞等待(pthread_cond_wait(&m_worker_cond,&m_lock)😉;

5 执行堵塞等待:
pthread_cond_wait(&m_worker_cond,&m_lock);//唤醒并往下执行代码。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

人在路上……

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值