mutex 互斥锁线程控制
来源:互联网 发布:强生婴儿知乎 编辑:程序博客网 时间:2024/05/12 19:57
一、引言
mutex是一种简单的加锁的方法来控制对共享资源的存取。这个互斥所只有两种状态:上锁和解锁。可以把互斥锁看成某种意义上的全局变量。在同一时刻,只能有一个线程掌握某个互斥锁,拥有上锁状态的线程能够对共享资源进行操作。若其它线程希望上锁一个已经上锁的互斥锁,则该线程就会挂起,直到上锁的线程释放该互斥锁为止。可以说,这把锁使得共享资源得以有序在各个线程中操作。
互斥锁主要操作:
1)初始化:pthread_mutex_init
2)上锁:pthread_mutex_lock
3)解锁:pthread_mutex_unlock
4)判断上锁:pthread_mutex_trylock
5)消除互斥锁:pthread_mutex_destroy
=========
问答
=========
1.可以有多个互斥锁吗?如何存在的?作用区间如何?
答:
二、实例
- /*mutex.c*/
- #include <stdio.h>
- #include <stdlib.h>
- #include <unistd.h>
- #include <pthread.h>
- #include <errno.h>
- pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
- int lock_var = 0;
- time_t end_time;
- void pthread1(void *arg);
- void pthread2(void *arg);
- int main(int argc, char *argv[])
- {
- pthread_t id1,id2;
- pthread_t mon_th_id;
- int ret;
- end_time = time(NULL)+10;
- pthread_mutex_init(&mutex,NULL);
- /*创建两个线程:pthread1,pthread2*/
- ret=pthread_create(&id1,NULL,(void *)pthread1, NULL);
- if(ret!=0)
- perror("pthread cread1");
- ret=pthread_create(&id2,NULL,(void *)pthread2, NULL);
- if(ret!=0)
- perror("pthread cread2");
- pthread_join(id1,NULL);
- pthread_join(id2,NULL);
- exit(0);
- }
- void pthread1(void *arg)
- {
- int i;
- while(time(NULL) < end_time)
- {
- /*互斥所上锁*/
- if(pthread_mutex_lock(&mutex)!=0)
- {
- perror("pthread_mutex_lock");
- }
- else
- printf("pthread1:pthread1 lock the variable\n");
- for(i=0;i<2;i++) //睡眠两个时间单位
- {
- sleep(1);
- lock_var++;
- }
- /*互斥所解锁*/
- if(pthread_mutex_unlock(&mutex)!=0)
- {
- perror("pthread_mutex_unlock");
- }
- else
- printf("pthread1:pthread1 unlock the variable\n");
- sleep(1); //睡眠1个时间单位
- }
- }
- void pthread2(void *arg)
- {
- int nolock=0;
- int ret;
- while(time(NULL) < end_time)
- {
- /*测试锁状态*/
- ret=pthread_mutex_trylock(&mutex);
- if(ret==EBUSY) //若为忙,表示被其它线程占用
- printf("pthread2:the variable is locked by pthread1\n");
- else
- {
- if(ret!=0)
- {
- perror("pthread_mutex_trylock");
- exit(1);
- }
- else
- printf("pthread2:pthread2 got lock.The variable is %d\n",lock_var);
- /*解锁*/
- if(pthread_mutex_unlock(&mutex)!=0)
- {
- perror("pthread_mutex_unlock");
- }
- else
- printf("pthread2:pthread2 unlock the variable\n");
- }
- sleep(3); //睡眠3个时间单位
- }
- }
运行结果如下:
- [root@localhost net]# ./mutex
- pthread1:pthread1 lock the variable
- pthread2:the variable is locked by pthread1
- pthread1:pthread1 unlock the variable
- pthread2:pthread2 got lock.The variable is 2
- pthread2:pthread2 unlock the variable
- pthread1:pthread1 lock the variable
- pthread1:pthread1 unlock the variable
- pthread2:pthread2 got lock.The variable is 4
- pthread2:pthread2 unlock the variable
- pthread1:pthread1 lock the variable
- pthread1:pthread1 unlock the variable
- pthread2:pthread2 got lock.The variable is 6
- pthread2:pthread2 unlock the variable
- pthread1:pthread1 lock the variable
- pthread1:pthread1 unlock the variable
- mutex 互斥锁线程控制
- mutex 互斥锁线程控制
- 线程通讯中的mutex线程控制详解
- mutex线程控制和信号量semaphore
- 线程间同步 之 互斥锁 mutex
- Windows线程同步之互斥锁(Mutex)
- Linux线程同步机制一--互斥锁mutex
- 线程同步:Mutex
- 线程-互斥量mutex
- 同步线程—Mutex
- 线程同步之MUTEX
- 线程(五)mutex
- 线程同步 mutex
- 线程同步---互斥量mutex
- 线程同步--互斥量Mutex
- 线程,Mutex简单用法
- 线程、进程通信---mutex=>互斥锁
- 线程间同步一 ———— 互斥锁(mutex)
- 好乐买总裁李树斌:我的第一次失败(Songtaste创始人)
- 第八周实验二
- 快排函数
- 英语学习之简单句
- 我的spring学习笔记2-IoC(反向控制 依赖注入)
- mutex 互斥锁线程控制
- 在linux中搭建网络服务的注意要点
- 我的spring学习笔记1-spring 简介
- 多线程编程(一):线程创建和退出
- 为什么你的微博营销效果不佳?
- UDP数据包大小问题
- Spring中事件处理de小技巧
- 多线程编程系列(五):“生产者消费者”实验——PV原语实现
- Java数字图像处理基础知识 - 必读