多个读写进程并行运行

概要

使用读写锁保护共享数据,创建了10个写线程和10个读线程。写线程每秒递增共享数据并打印结果,读线程每秒读取共享数据并打印结果。主线程则一直休眠以保持程序运行。

#include <pthread.h>
#include <stdio.h>
#include <unistd.h>
#include <semaphore.h>
#include <string.h>

pthread_rwlock_t rwlock = PTHREAD_RWLOCK_INITIALIZER; // 初始化读写锁
int shared_data = 0; // 共享数据

/* 写线程函数 */
static void *my_thread_write_func (void *data)
{
	int i = (int)data; // 将传入的参数转换为整型
	while (1)
	{
		pthread_rwlock_wrlock(&rwlock); // 获取写锁
		shared_data++; // 修改共享数据
		printf("Thread %d is modifying the shared data: %d\n", i, shared_data); // 打印当前线程编号和共享数据
		pthread_rwlock_unlock(&rwlock); // 释放写锁
		sleep(1); // 休眠1秒		
	}

	return NULL; // 返回空指针
}

/* 读线程函数 */
static void *my_thread_read_func (void *data)
{
	int i = (int)data; // 将传入的参数转换为整型
	while (1)
	{
		pthread_rwlock_rdlock(&rwlock); // 获取读锁
		printf("Thread %d is reading the shared data: %d\n", i, shared_data); // 打印当前线程编号和共享数据
		pthread_rwlock_unlock(&rwlock); // 释放读锁
		sleep(1); // 休眠1秒		
	}

	return NULL; // 返回空指针
}

/* 主函数 */
int main(int argc, char **argv)
{
	pthread_t tid; // 线程标识符
	int ret; // 用于接收返回值
	int i; 
	
	/* 创建写线程 */
	for (i = 0; i < 10; i++)
	{
		ret = pthread_create(&tid, NULL, my_thread_write_func, (void *)i); // 创建写线程
		if (ret)
		{
			printf("pthread_create err!\n"); // 如果创建线程失败,打印错误信息
			return -1; // 返回错误代码
		}
	}

	/* 创建读线程 */
	for (i = 0; i < 10; i++)
	{
		ret = pthread_create(&tid, NULL, my_thread_read_func, (void *)i); // 创建读线程
		if (ret)
		{
			printf("pthread_create err!\n"); // 如果创建线程失败,打印错误信息
			return -1; // 返回错误代码
		}
	}

	while (1)
	{
		sleep(100); // 主线程休眠,保持程序运行
	}
	
	pthread_rwlock_destroy(&rwlock); // 销毁读写锁
	return 0; // 返回0表示程序正常结束
}

实验效果

ktj:~/study/$ ./pthread7.out 
Thread 0 is modifying the shared data: 1
Thread 2 is modifying the shared data: 2
Thread 1 is modifying the shared data: 3
Thread 4 is modifying the shared data: 4
Thread 3 is modifying the shared data: 5
Thread 5 is modifying the shared data: 6
Thread 7 is modifying the shared data: 7
Thread 6 is modifying the shared data: 8
Thread 8 is modifying the shared data: 9
Thread 9 is modifying the shared data: 10
Thread 0 is reading the shared data: 10
Thread 1 is reading the shared data: 10
Thread 2 is reading the shared data: 10
Thread 3 is reading the shared data: 10
Thread 4 is reading the shared data: 10
Thread 5 is reading the shared data: 10
Thread 6 is reading the shared data: 10
Thread 8 is reading the shared data: 10
Thread 7 is reading the shared data: 10
Thread 9 is reading the shared data: 10
Thread 1 is modifying the shared data: 11
Thread 4 is modifying the shared data: 12
Thread 2 is modifying the shared data: 13
Thread 0 is modifying the shared data: 14
Thread 3 is modifying the shared data: 15
Thread 7 is modifying the shared data: 16
Thread 8 is modifying the shared data: 17
Thread 6 is modifying the shared data: 18
Thread 9 is modifying the shared data: 19
Thread 0 is reading the shared data: 19
Thread 1 is reading the shared data: 19
Thread 2 is reading the shared data: 19
Thread 4 is reading the shared data: 19
Thread 3 is reading the shared data: 19
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值