多线程 一个mutex互斥量实例(unix环境高级编程)

本文详细介绍了并发编程中使用互斥锁实现线程安全的机制,通过实例展示了如何在多个线程间共享资源的同时避免数据竞争问题。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >


/*
 * mutex.c
 *
 *  Created on: 2011-11-23
 *      Author: lc
 */

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

struct foo {
	int count;
	pthread_mutex_t mutex;
};
struct foo * fp;
struct foo * init_foo() {
	struct foo * fp;

	if ((fp = (struct foo *) malloc(sizeof(struct foo))) != NULL) {
		fp->count = 1;
		//初始化mutex失败
		if (pthread_mutex_init(&fp->mutex, NULL) != 0) {
			pthread_mutex_destroy(&fp->mutex);
			free(fp);
			return NULL;
		}
	}

	return fp;
}

/*
 * add reference to foo
 */
void attach_foo(struct foo * fp) {
	pthread_mutex_lock(&fp->mutex);
	fp->count++;
	fprintf(stderr, "%d thread has attached foo in thread %d\n", fp->count,(unsigned int)pthread_self());
	sleep(1);
	pthread_mutex_unlock(&fp->mutex);
}

/*
 * release reference to foo
 * 如果count的值最后是0,说明已经没有线程引用该对象,先删除mutex互斥量,然后free掉fp的内存
 */
void detach_foo(struct foo * fp) {
	int count;
	pthread_mutex_lock(&fp->mutex);
	count == --(fp->count);
	pthread_mutex_unlock(&fp->mutex);

	if (count == 0) {

		pthread_mutex_destroy(&fp->mutex);
		free(fp);
	}
}

void * func1(void * arg) {
	attach_foo(fp);
}
void * func2(void * arg) {
	attach_foo(fp);
}

void * func3(void *arg) {
	attach_foo(fp);
}

int main(int argc, char **argv) {
	int ret;
	pthread_t tid1, tid2, tid3;

	fp = init_foo();
	fprintf(stderr, "%d thread has attached foo\n", fp->count);

	ret = pthread_create(&tid1, NULL, func1, NULL);
	if (ret != 0) {
		fprintf(stderr, "%s\n", strerror(errno));
	}
	ret = pthread_create(&tid2, NULL, func2, NULL);
	if (ret != 0) {
		fprintf(stderr, "%s\n", strerror(errno));

	}

	ret = pthread_create(&tid3, NULL, func3, NULL);
	if (ret != 0) {
		fprintf(stderr, "%s\n", strerror(errno));

	}
	sleep(4);
	fprintf(stderr, "%d thread has attached foo\n", fp->count);
	return 0;
}


输出:

snape@snape-virtual-machine:~/桌面$ gcc -o mutex mutex.c -lpthread
snape@snape-virtual-machine:~/桌面$ ./mutex 
1 thread has attached foo
2 thread has attached foo in thread -1215399056
3 thread has attached foo in thread -1223791760
4 thread has attached foo in thread -1232184464
4 thread has attached foo


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值