当前位置:网站首页>互斥量互斥锁
互斥量互斥锁
2022-06-29 07:19:00 【Lee Neo】
芹菜笔记:
这里面线程池和令牌桶没有学习;
调试小技巧:统计程序消耗的时间;

互斥量:

原子笔记
互斥锁初始化:
pthread_mutex_init()函数对互斥锁进行初始化
pthread_mutex_t mutex;
pthread_mutex_init(&mutex, NULL);
或者:
pthread_mutex_t *mutex = malloc(sizeof(pthread_mutex_t));
pthread_mutex_init(mutex, NULL);互斥锁加锁和解锁
pthread_mutex_lock()可以对互斥锁加锁、获取互斥锁;pthread_mutex_unlock()可以对互斥锁解锁、释放互斥锁;
如果线程不希望被阻塞,可以使用 pthread_mutex_trylock()函数
销毁互斥锁
pthread_mutex_destroy()函数来销毁互斥锁
- 不能销毁还没有解锁的互斥锁,否则将会出现错误;
- 没有初始化的互斥锁也不能销毁
互斥锁死锁
// 线程 A
pthread_mutex_lock(mutex1);
pthread_mutex_lock(mutex2);
// 线程 B
pthread_mutex_lock(mutex2);
pthread_mutex_lock(mutex1);互斥锁属性
pthread_mutex_init()函数初始化互斥锁时可以设置互斥锁的属性
pthread_mutexattr_destroy()将其销毁
pthread_mutexattr_gettype()函数得到互斥锁类型属性,
pthread_mutexattr_settype()修改/设置互斥锁类型属性,
pthread_mutex_t mutex;
pthread_mutexattr_t attr;
/* 初始化互斥锁属性对象 */
pthread_mutexattr_init(&attr);
/* 将类型属性设置为 PTHREAD_MUTEX_NORMAL */
pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_NORMAL);
/* 初始化互斥锁 */
pthread_mutex_init(&mutex, &attr);
......
/* 使用完之后 */
pthread_mutexattr_destroy(&attr);
pthread_mutex_destroy(&mutex);面试题:依次打印abcd
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <pthread.h>
#include <unistd.h>
#define THRNUM 4
static pthread_mutex_t mut[THRNUM];
static int next(int n)
{
if(n + 1 == THRNUM)
return 0;
return n + 1;
}
static void *thr_func(void *p)
{
int c;
int n = (int)p;
c = 'a' + n;
while(1)
{
pthread_mutex_lock(mut+n);
write(1, &c, 1);
pthread_mutex_unlock(mut+next(n));
}
pthread_exit(NULL);
}
int main()
{
pthread_t tid[THRNUM];
int i, err;
for(i = 0; i < THRNUM; i++)
{
pthread_mutex_init(mut+i, NULL);
pthread_mutex_lock(mut+i);
err = pthread_create(tid+i, NULL, thr_func, (void *)i);
if(err)
{
fprintf(stderr, "pthread_create():%s\n", strerror(err));
exit(1);
}
}
pthread_mutex_unlock(mut+0);
alarm(2 );
for(i = 0; i < THRNUM; i++)
pthread_join(tid[i], NULL);
exit(0);
}边栏推荐
猜你喜欢

Swift中@dynamicMemberLookup和callAsFunction特性实现对象透明代理功能
![[eye of depth wuenda machine learning homework class phase IV] regularization regularization summary](/img/24/3d0b892c0eaa330f0c69764de5da13.png)
[eye of depth wuenda machine learning homework class phase IV] regularization regularization summary
![[Kerberos] analysis of Kerberos authentication](/img/c5/d429bcf3c26d9476531362ef286734.png)
[Kerberos] analysis of Kerberos authentication

Thread pool operations in cartographer
![[6G] collation of white paper on computing network technology](/img/a3/8e60eef55ebcd91fa6188722c87a70.png)
[6G] collation of white paper on computing network technology

x86和x64的区别

常见的七大排序

Linear regression with one variable

表格背单词的方法

壁纸小程序源码双端微信抖音小程序
随机推荐
Codeforces Round #799 (Div. 4)
Manipulate the outer DOM in the iframe tag
语音标注工具:Praat
Excel中VLOOKUP函数简易使用——精确匹配或近似匹配数据
[quantitative investment system]django realizes screening and paging from the database
[eye of depth wuenda machine learning homework class phase IV] summary of logistic regression
JS XOR obfuscation code
MySQL中有哪些约束?(实例验证)
馬賽克筆記
STM32基于HAL库的USART+DMA使用
想炒股开户,在网上进行股票开户安全吗?-
新版付费塔罗牌测算源码 (附带搭建教程)
一个高频问题,三种模型思维来破解此风控难题
Talking about Nacos configuration center from Nacos client
802.11--802.11n协议 PHY
IndexTree以及应用
关于#sql#的问题:创建一个名为View_XB视图,功能是①如果有重名的视图先删除后创建②显示XSB表中本班级的男女生各有多少人,并添加检查约束
在colaboratory上云端使用GPU训练(以YOLOv5举例)
New spark in intelligent era: wireless irrigation with Lora wireless transmission technology
MySQL enable logging