Linux多线程 |互斥锁|条件变量

本文通过一个简单的线程同步示例,展示了如何使用条件变量和互斥锁确保多线程环境中全局共享字符串的安全访问。首先介绍了无同步机制下线程执行的不确定性,随后详细解释了如何通过加锁和条件变量来实现线程间的有序执行。
摘要由CSDN通过智能技术生成

一、练习要求

三个线程访问一个全局共享的字符串,thread1访问时会在这个字符串末尾追加'1'字符,thread2访问时会在这个字符串末尾追加'2'字符,thread3访问时会在这个字符串末尾追加'3‘字符。要求:thread1-thread2-thread3依次访问,最终字符串的内容为"123"。

二、第一阶段

在main函数中,开启这三个线程。并没有为线程访问共享数据加锁和同步。在main函数中sleep(3)来避免主线程过早地退出。

【实现代码】

/*
 * ThreadTest_1.c
 *
 *  Created on: 2011-7-13
 *      Author: zhangyun
 */
#include<stdio.h>
#include<stdlib.h>
#include<pthread.h>
#include<errno.h>
#include<unistd.h>
#include<string.h>

char g_array[5]= {'\0'};

void* thread1(void*);
void* thread2(void*);
void* thread3(void*);

int main(int argc, char** argv)
{
	printf("[ enter main ]\n");
	pthread_t tid1, tid2, tid3;
	int rc1=0, rc2=0, rc3=0;

	rc3 = pthread_create(&tid3, NULL, thread3, NULL);
	if(rc3 != 0)
		printf("%s: %d\n",__func__, strerror(rc3));

	rc2 = pthread_create(&tid2, NULL, thread2, NULL);
	if(rc2 != 0)
		printf("%s: %d\n",__func__, strerror(rc2));

	rc1 = pthread_create(&tid1, NULL, thread1, &tid2);
	if(rc1 != 0)
		printf("%s: %d\n",__func__, strerror(rc1));

	sleep(3);
	printf("[ leave main ]\n");
	exit(0);
}

void* thread1(void* arg)
{
	printf("[ enter thread1 ]\n");
	printf("this is thread1, g_array: %s, thread id is %u\n",g_array, (unsigned int)pthread_self());
	int index = strlen(g_array);
	g_array[index] = '1';
	printf("this is thread1, g_array: %s, thread id is %u\n",g_array, (unsigned int)pthread_self());
	printf("[ leave thread1 ]\n");
	pthread_exit(0);
}

void* thread2(void* arg)
{
	printf("[ enter thread2 ]\n");
	printf("this is thread2, g_array: %s, thread id is %u\n",g_array, (unsigned int)pthread_self());
	int index = strlen(g_array);
	g_array[index] = '2';
	printf("this is thread2, g_array: %s, thread id is %u\n",g_array, (unsigned int)pthread_self());
	printf("[ leave thread2 ]\n");
	pthread_exit(0);
}

void* thread3(void* arg)
{
	printf("[ enter thread3 ]\n");
	printf("this is thread3, g_array: %s, thread id is %u\n",g_array, (unsigned int)pthread_self());
	int index = strlen(g_array);
	g_array[index] = '3';
	printf("this is thread3, g_array: %s, thread id is %u\n",g_array, (unsigned int)pthread_self());
	printf("[ leave thread3 ]\n");
	pthread_exit(0);
}

【编译和运行命令】

gcc -lpthread ThreadTest_1.c

./a.out 

【运行结果】
[ enter main ]
[ enter thread1 ]
[ enter thread3 ]
[ enter thread2 ]
this is thread1, g_array: , thread id is 3060820848
this is thread2, g_array: , thread id is 3069213552
this is thread2, g_array: 12, thread id is 3069213552
[ leave thread2 ]
this is thread3, g_array: 12, thread id is 3077606256
this is thread3, g_array: 123, thread id is 3077606256
[ leave thread3 ]
this is thread1, g_array: 123, thread id is 3060820848
[ leave thread1 ]
[ leave main ]

二、第二阶段

为线程访问共享数据加锁和同步

1. thread1访问共享数据完毕后让cond1条件为真;

2. thread2等待cond1条件为真后才进行数据访问,访问数据完毕时让cond2条件为真;

3. thread3等待cond2条件为真后才进行数据访问,访问数据完毕时让cond3条件为真;

4.主线程等待cond3条件为真才退出。

注意:在对临界资源进行操作之前需要加锁,操作完解锁使用条件变量可以以原子方式阻塞线程,直到某个特定条件为真为止。

为什么条件变量始终与互斥锁一起使用,对条件的测试是在互斥锁(互斥)的保护下进行的呢?

因为“某个特性条件”通常是在多个线程之间共享的某个变量。互斥锁允许这个变量可以在不同的线程中设置和检测。

【实现代码】

/*
 * ThreadTest_1.c
 *
 *  Created on: 2011-7-13
 *      Author: zhangyun
 */
#include<stdio.h>
#include<stdlib.h>
#include<pthread.h>
#include<errno.h>
#include<unistd.h>
#include<string.h>

char g_array[5]= {'\0'};

static pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER; //mutex
static pthread_cond_t cond1 = PTHREAD_COND_INITIALIZER; //cond
static pthread_cond_t cond2 = PTHREAD_COND_INITIALIZER; //cond
static pthread_cond_t cond3= PTHREAD_COND_INITIALIZER; //cond

static pthread_cond_t cond_main_exit= PTHREAD_COND_INITIALIZER; //cond

void* thread1(void*);
void* thread2(void*);
void* thread3(void*);

int main(int argc, char** argv)
{
	printf("[ enter main ]\n");
	pthread_t tid1, tid2, tid3;
	int rc1=0, rc2=0, rc3=0;

	
	rc3 = pthread_create(&tid3, NULL, thread3, NULL);
	if(rc3 != 0)
		printf("%s: %d\n",__func__, strerror(rc3));
	
	rc2 = pthread_create(&tid2, NULL, thread2, NULL);
	if(rc2 != 0)
		printf("%s: %d\n",__func__, strerror(rc2));

	rc1 = pthread_create(&tid1, NULL, thread1, &tid2);
	if(rc1 != 0)
		printf("%s: %d\n",__func__, strerror(rc1));

	pthread_mutex_lock(&mutex);
	pthread_cond_wait(&cond3, &mutex);
	pthread_mutex_unlock(&mutex);
	printf("[ leave main ]\n");
	exit(0);

	
}

void* thread1(void* arg)
{
	printf("[ enter thread1 ]\n");
	
	pthread_mutex_lock(&mutex);
	//printf("this is thread1, g_array: %s, thread id is %u\n",g_array, (unsigned int)pthread_self());
	int index = strlen(g_array);
	sleep(5);
	if(index == 0)
	{
		g_array[index] = '1';
		
	}
	printf("this is thread1, g_array: %s, thread id is %u\n",g_array, (unsigned int)pthread_self());
	pthread_cond_signal(&cond1);
	pthread_mutex_unlock(&mutex);
	
	printf("[ leave thread1 ]\n");
	pthread_exit(0);
}

void* thread2(void* arg)
{
	

	printf("[ enter thread2 ]\n");
	
	pthread_mutex_lock(&mutex); // why cant lock ???
	pthread_cond_wait(&cond1, &mutex);
	//printf("this is thread2, g_array: %s, thread id is %u\n",g_array, (unsigned int)pthread_self());	
	int index = strlen(g_array);
	sleep(3);
	if(index == 1)
	{
		g_array[index] = '2';	
	}
	printf("this is thread2, g_array: %s, thread id is %u\n",g_array, (unsigned int)pthread_self());
	pthread_cond_signal(&cond2);
	pthread_mutex_unlock(&mutex);
	
	printf("[ leave thread2 ]\n");
	pthread_exit(0);
}

void* thread3(void* arg)
{
	printf("[ enter thread3 ]\n");
	pthread_mutex_lock(&mutex);
	pthread_cond_wait(&cond2, &mutex);
	//printf("this is thread3, g_array: %s, thread id is %u\n",g_array, (unsigned int)pthread_self());
	
	int index = strlen(g_array);
	if(index == 2)
	{
		g_array[index] = '3';
		pthread_cond_signal(&cond3);
	}
	printf("this is thread3, g_array: %s, thread id is %u\n",g_array, (unsigned int)pthread_self());
	pthread_mutex_unlock(&mutex);
	printf("[ leave thread3 ]\n");
	pthread_exit(0);
}
【运行结果】

[ enter main ]
[ enter thread3 ]
[ enter thread2 ]
[ enter thread1 ]
this is thread1, g_array: 1, thread id is 3065633680
[ leave thread1 ]
this is thread2, g_array: 12, thread id is 3076123536
[ leave thread2 ]
this is thread3, g_array: 123, thread id is 3086613392
[ leave thread3 ]
[ leave main ]


评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值