/* * 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