linux 互斥量
来源:互联网 发布:淘宝数据魔方专业版 编辑:程序博客网 时间:2024/05/13 10:23
互斥量从本质上讲是一把锁,在访问共享资源前对互斥量加锁,在访问完成后释放互斥量。对互斥量进行加锁后,任何其他试图再次对互斥量加锁的线程都会被阻塞直到当前线程释放该互斥锁。如果释放互斥量时有一个以上的线程阻塞,那么所有该锁上的阻塞线程都会变成可运行状态,第一个变为运行的线程就可以对互斥量进行加锁,其他线程就会看到互斥量依然是锁着的,只能再次等待该互斥量重新变成可有。在这种方式下,每次只有一个线程可以向前执行。
互斥量是用pthread_mutex_t数据类型表示的。
初始化及销毁:
pthread_mutex_init(pthread_mutex_t *restrict mutex, const pthread_mutexattr_t *restrictattr);
pthread_mutex_destroy(pthread_mutex_t *mutex);
加锁及解锁:
pthread_mutex_lock(pthread_mutex_t *mutex);
pthread_mutex_lock(pthread_mutex_t *mutex);
pthread_mutex_unlock(pthread_mutex_t *mutex);
一个demo:
#include <stdlib.h>#include <stdio.h>#include <pthread.h>//需要同步保护的结构体struct foo {int f_cnt;pthread_mutex_t f_lock;int f_id;int f_data;};//alloct the objstruct foo *foo_alloc(int id){struct foo *fp;if ((fp = malloc(sizeof(struct foo))) != NULL){fp->f_cnt = 1;fp->f_id = id;if (pthread_mutex_init(&fp->f_lock, NULL) != 0){free(fp);fp = NULL;return NULL;}}return fp;}//add a reference to the objvoid foo_hold(struct foo *fp){pthread_mutex_lock(&fp->f_lock);fp->f_cnt++;pthread_mutex_unlock(&fp->f_lock);}//release a reference to the objvoid foo_rele(struct foo *fp){pthread_mutex_lock(&fp->f_lock);if (--fp->f_cnt == 0)//last reference{pthread_mutex_unlock(&fp->f_lock);pthread_mutex_destroy(&fp->f_lock);free(fp);}else{pthread_mutex_unlock(&fp->f_lock);}}void *thread1(void *args){struct foo *fp = (struct foo*)args;while(fp->f_data <= 200){foo_hold(fp);fp->f_data++;printf("thread1 f_data = %d\n", fp->f_data);foo_rele(fp);sleep(1);}}void *thread2(void *args){struct foo *fp = (struct foo*)args;while (fp->f_data <= 200){foo_hold(fp);fp->f_data++;printf("thread2 f_data = %d\n", fp->f_data);foo_rele(fp);sleep(1);}}int main(){pthread_t tid1, tid2;struct foo *fp = foo_alloc(1);pthread_create(&tid1, NULL, thread1, (void*)fp);pthread_create(&tid2, NULL, thread2, (void*)fp);pthread_join(tid1, NULL);exit(0);}
0 0
- linux 互斥量
- Linux 进程互斥量
- Linux 进程互斥量
- Linux c 多线程 互斥量
- linux多线程 -- 互斥量
- Linux线程同步---互斥量
- Linux线程同步------互斥量
- Linux--多线程之互斥量
- Linux 同步机制:互斥量
- Linux Kernel 互斥量
- Linux Kernel 互斥量初始化
- linux
- linux
- Linux
- Linux
- LINUX!
- Linux
- linux
- 傅立叶变换的本质
- POI生成(Excel 2007).xlsx文件时报“java.lang.NoSuchMethodError”错误的解决
- Edittext密码与显示状态切换
- OpenCV | Mat - IplImage 转换
- 解决安装完Ubuntu后,有线网卡,安装无线兼容包
- linux 互斥量
- Spring MVC 框架中Spring扫描问题
- HDOJ1722--Cake
- PreferenceFragment修改边距
- python图形处理库Python Imaging Library (PIL)简介及安装
- java数据类型
- cocos2d - JS 计时器(schedule)
- Python列表数组字典
- php根据汉字获取拼音(php基于拼音搜索实现原理)