<linux线程>同步互斥机制之【互斥锁】
来源:互联网 发布:sql 去重 编辑:程序博客网 时间:2024/05/17 06:05
本文基于信号量
如果信号量的值最多为1,那实际上相当于一个共享资源在任意时刻最多只能有一个线程在访问,这样的逻辑称为“互斥”。这时,有一种更加方便和语义更加准确的工具来满足这种逻辑---互斥锁。几个线程同时去抢一个锁,谁抢到了锁就进门把房间锁上,访问数据,出来后之前解锁。
照这样的逻辑,互斥锁的操作无非就是:
1.定义互斥锁。-----pthread_mutex_t类型
2.初始化锁。--------pthread_mutex_init()函数 int pthread_mutex_init(pthread_mutex_t *restrict mutex, const pthread_mutexattr_t *restrict attr);
3.加锁。--------------pthread_mutex_lock()函数 int pthread_mutex_lock(pthread_mutex_t *mutex)
4.解锁。--------------pthread_mutex_unlock()函数 int pthread_mutex_unlock(pthread_mutex_t *mutex)
5.销毁互斥锁。----pthread_mutex_destroy()函数 int pthread_mutex_destroy(pthread_mutex_t *mutex)
C代码示例:
#include <stdio.h>#include <pthread.h>#include <unistd.h>/* 1 定义互斥锁变量 */pthread_mutex_t m;// 被访问的资源,全局变量int a = 100;void *routine(void *arg){while (1){/* 3 在访问资源之前,加锁 */pthread_mutex_lock(&m);// 访问资源printf("副1线程获得锁,读到资源:a=%d,占用1秒\n", a);sleep(1);/* 4 访问完后,解锁 */pthread_mutex_unlock(&m);}// 退出线程pthread_exit(NULL);}void *routine1(void *arg){while (1){/* 3 在访问资源之前,加锁 */pthread_mutex_lock(&m);// 访问资源printf("副2线程获得锁,读到资源:a=%d,占用2秒\n", a);sleep(2);/* 4 访问完后,解锁 */pthread_mutex_unlock(&m);}// 退出线程pthread_exit(NULL);}int main(int argc, char const *argv[]){/* 2 初始化互斥锁 */pthread_mutex_init(&m, NULL);// 创建线程pthread_t tid, tid1;pthread_create(&tid, NULL, routine, NULL);pthread_create(&tid1, NULL, routine1, NULL);sleep(1);while (1){/* 3 在访问资源之前,加锁 */pthread_mutex_lock(&m);// 访问资源printf("主线程抢到锁,访问到到资源:a=%d, 占用3秒。\n", a);sleep(3);/* 4 访问完后,解锁 */pthread_mutex_unlock(&m);// 解锁完后休息一下,不然近水楼台先得月,会一直被主线程抢到,而且主线程会有优先权sleep(1);printf("\033[42m我休息一下,你们先抢!!\033[0m\n");}// 阻塞等待子线程退出、pthread_join(tid, NULL);pthread_join(tid1, NULL);/* 5 销毁互斥锁 */pthread_mutex_destroy(&m);return 0;}
阅读全文
0 0
- <linux线程>同步互斥机制之【互斥锁】
- <linux线程>同步互斥机制之【读写锁】
- <linux线程>同步互斥机制之【条件变量】
- Linux之线程:同步与互斥
- Linux 线程同步机制:互斥、读写锁、条件变量
- linux线程互斥与同步---互斥锁
- linux线程互斥与同步---互斥锁
- linux 线程同步、互斥
- Linux线程浅析[线程的同步和互斥之线程互斥锁]
- 线程的同步、互斥机制
- Linux 编程之【线程】同步与互斥
- Linux--线程同步与互斥之条件变量
- Linux--线程同步与互斥之读写锁
- Linux线程浅析[线程的同步和互斥之线程同步的条件变量pthread_cond_t]
- linux互斥与同步 之 互斥锁
- linux ------ 资源互斥与同步机制
- 【Linux系统编程】线程同步与互斥:互斥锁
- linux线程同步与互斥
- iOS Swift Crash的捕获
- 关于xib的连线
- npm install --save 与 npm install --save-dev 的区别
- iPhone X 适配
- 基于TensorFlow的车牌号识别系统
- <linux线程>同步互斥机制之【互斥锁】
- React Native 自定义modal
- JAVA 常见的异常
- mybatisy异常 Parameter 'xxx' not found. Available parameters are[1, 0, param1, param2]
- 最新版的struts-2.5.13 配置的时候出现问题和解决方案
- Android JNI开发(2)--native方法调用Java方法
- 动态区间第K小数 分块/树套树
- CSS+HTML实现倾斜导航条
- Shell脚本数组操作小结