linux程序设计——用互斥量进行同步(第十二章)

58 篇文章 0 订阅
57 篇文章 9 订阅
12.5.2    用互斥量进行同步
另一种在多线程程序中的同步访问方法是使用 互斥量.它 允许程序员锁住某个对象,使得每次只能有一个线程访问它.为了控制对关键代码的访问,必须在进入这段代码之前锁住一个互斥量,然后在完成操作之后解锁它.
用于互斥量的基本函数和用于信号量的函数非常相似,它们的定义如下所示:
#include <pthread.c>
int pthread_mutex_init(pthread_mutex_t *mutex, const pthread_mutexattr_t *mutexattr);
int pthread_mutex_lock(pthread_mutex_t *mutex);
int pthread_mutex_unlock(pthread_mutex_t *mutex);
int pthread_mutex_destroy(pthread_mutex_t *mutex);
与信号量相似,这些函数的参数都是一个先前声明过的对象的指针.对互斥量来说,这个对象的类型为pthread_mutex_t.pthread_mutex_init函数中的属性参数默认为fast,一般传递参数NULL保持默认属性.
编写程序thread4.c,假设需要保护对一些关键变量的访问,用一个互斥量来保证任一时刻只能有一个线程访问它们.为了让实力代码容易阅读,省略了对互斥量加锁和解锁调用的返回值应该进行的一些错误检查.在软件代码中,对返回值的检查是必不可少的.
/*************************************************************************
 > File Name:    thread4.c
 > Description:  thread4.c通过互斥量来保证任一时刻只有一个线程访问字符数组work_area(对它进行读(统计)/写)
 > Author:       Liubingbing
 > Created Time: 2015年07月05日 星期日 20时47分06秒
 > Other:        thread4.c省略了对互斥量加锁和解锁调用的返回值应该进行的一些错误检查
 ************************************************************************/

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

void *thread_function(void *arg);
pthread_mutex_t work_mutex; /* protects both work_acea and time_to_exit */

#define WORK_SIZE 1024
char work_area[WORK_SIZE];
int time_to_exit = 0;

int main(){
	int res;
	pthread_t a_thread;
	void *thread_result;

	/* pthread_mutex_init初始化互斥量work_mutex */
	res = pthread_mutex_init(&work_mutex, NULL);
	if (res != 0) {
		perror("Mutex initialization failed");
		exit(EXIT_FAILURE);
	}

	/* pthread_create创建新线程 
	 * a_thread中保存新线程的标识符, thread_function为新线程启动调用的函数 */
	res = pthread_create(&a_thread, NULL, thread_function, NULL);
	if (res != 0) {
		perror("Thread creation failed");
		exit(EXIT_FAILURE);
	}

	/* pthread_mutex_lock对互斥量work_mutex加锁,如果它已经被锁住,则这个调用将被阻塞直到它被释放为止 */
	pthread_mutex_lock(&work_mutex);
	printf("Input some text. Enter 'end' to finish\n");
	while (!time_to_exit) {
		fgets(work_area, WORK_SIZE, stdin);
		/* pthread_mutex_unlock对互斥量work_mutext解锁 */
		pthread_mutex_unlock(&work_mutex);
		while (1) {
			/* pthread_mutex_lock对互斥量work_mutex加锁 */
			pthread_mutex_lock(&work_mutex);
			/* 如果work_area第一个字符不为null,则对互斥量work_mutex解锁,然后等待新线程执行 */
			if (work_area[0] != '\0') {
				pthread_mutex_unlock(&work_mutex);
				sleep(1);
			} else {
				break;
			}
		}
	}
	pthread_mutex_unlock(&work_mutex);
	printf("\nWaiting for thread to finish...\n");
	res = pthread_join(a_thread, &thread_result);
	if (res != 0) {
		perror("Thread join failed");
		exit(EXIT_FAILURE);
	}
	printf("Thread joined\n");
	pthread_mutex_destroy(&work_mutex);
	exit(EXIT_SUCCESS);
}

void *thread_function(void *arg) {
	sleep(1);
	/* 首先试图对互斥量work_mutex加锁,如果它已经被锁住,这个调用将被阻塞直到它释放为止. */
	pthread_mutex_lock(&work_mutex);
	while (strncmp("end", work_area, 3) != 0) {
		printf("You input %d characters\n", strlen(work_area) - 1);
		/* 通过将第一个字符设置为null的方法来通知读取输入的线程, 新线程完成了字符统计 */
		work_area[0] = '\0';
		/* 对互斥量work_mutex解锁, 并等待主线程继续运行*/
		pthread_mutex_unlock(&work_mutex);
		sleep(1);
		/* 对互斥量work_mutex加锁, 如果加锁成功就检查是否主线程又有字符送来要处理 */
		pthread_mutex_lock(&work_mutex);
		while (work_area[0] == '\0') {
			/* 如果没有字符要处理,就解锁互斥量,继续等待 */
			pthread_mutex_unlock(&work_mutex);
			sleep(1);
			pthread_mutex_lock(&work_mutex);
		}
	}
	/* 如果work_area="end",则设置time_to_exit = 1, 之后退出 */
	time_to_exit = 1;
	work_area[0] = '\0';
	pthread_mutex_unlock(&work_mutex);
	pthread_exit(0);
}
新线程首先对等待1秒,然后对互斥量work_mutex加锁,如果work_mutex已经加锁,则这个调用将被阻塞直到互斥量被时释放为止.一旦获得访问权,先判断work_area是否为end.
    如果是,则设置time_to_exit=1...然后退出.
    如果不是,则统计字符数,设置work_area第一个字符为null(通过设置第一个字符为null,来等待主线程读取新的字符),对互斥量work_mutex解锁,然后等待主线程,接着对互斥量加锁.然后循环判断第一个字符是否仍然为null.
        如果是,则继续等待主线程(通过解锁,休眠,加锁),
        如果不是(说明主线程读取了新的字符),则进行字符统计.
主线程首先对互斥量work_mutex加锁,然后通过time_to_exit判断是否跳出循环,如果不跳出循环,则接着读取字符,解锁互斥量,然后循环等待新线程,通过加锁互斥量,然后判断work_area第一个字符是否为空.
    如果不是,则跳出循环,读取新的字符.
    如果是,则解锁互斥量,休眠.继续循环.

使用流程图会更好看一点,以后看看找找画流程图比较方便的软件.此外这种通过轮询来获得结果的方法通常并不是很好的变成方式.在实际的编程中,应该尽可能用信号量来避免出现这种情况.

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值