当前位置:网站首页>互斥量互斥锁
互斥量互斥锁
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);
}边栏推荐
- 线段树以及使用
- php 清除多维数组里面的空值
- 关于SqlSugar的多对多的级联插入的问题(无法获取集合属性的id,导致无法维护中间表)
- [quantitative investment system] problem records and Solutions
- NLP annotation tool: label studio realizes multi-user collaborative marking
- NOR flash application layer operation
- aws iam内联策略示例
- Friends, static keywords, static methods, and relationships between objects
- 语音标注自动音段对齐工具SPPAS使用笔记
- 1284_ Implementation analysis of FreeRTOS task priority acquisition
猜你喜欢

Ceres optimization based on sophus

语音合成:概述【不等长序列关系建模的生成任务】

SizeBalanceTree

Notice on organizing the second round of the Northwest Division (Shaanxi) of the 2021-2022 National Youth electronic information intelligent innovation competition

JSP learning part

sql语句concat搜索不出结果

MySQL system keyword summary (official website)

Stm32 usart+dma usage based on Hal Library

Code:: blocks code formatting shortcuts
![[eye of depth wuenda machine learning operation class phase IV] logic regression programming implementation](/img/48/0ed9b9b2adbce6ab3cf4a07674a9f4.png)
[eye of depth wuenda machine learning operation class phase IV] logic regression programming implementation
随机推荐
NLP annotation tool: label studio realizes multi-user collaborative marking
Basics - syntax standards (ANSI C, ISO C, GNU C)
Notice on organizing the second round of the Northwest Division (Shaanxi) of the 2021-2022 National Youth electronic information intelligent innovation competition
Environmental preparation - Engineering Management
What are the constraints in MySQL? (instance verification)
语音处理工具:sox
Debugging nocturnal simulator with ADB command
[hcie TAC] question 5-2
Sonic communication - streaming data processing - window alignment
Want to open a stock account, is it safe to open a stock account online-
Using method and de duplication of distinct() in laravel
华为云的AI深潜之旅
Development trend of garment industry | supply chain | intelligent manufacturing
Exercise - select sort
[domain penetration authorization] cve-2020-1472 Netlogon privilege escalation vulnerability
穿越过后,她说多元宇宙真的存在
A review of visual SLAM methods for autonomous driving vehicles
Program debugging - debug/release version
Flutter shared_preferences使用
Should product managers learn from ink knife or Axure?