当前位置:网站首页>Mutex mutex
Mutex mutex
2022-06-29 08:24:00 【Lee Neo】
Celery notes :
The thread pool and token bucket are not learned ;
Debugging tips : Count the time consumed by the program ;

The mutex :

Atomic notes
Mutex initialization :
pthread_mutex_init() Function to initialize the mutex
pthread_mutex_t mutex;
pthread_mutex_init(&mutex, NULL);
perhaps :
pthread_mutex_t *mutex = malloc(sizeof(pthread_mutex_t));
pthread_mutex_init(mutex, NULL);Mutex locking and unlocking
pthread_mutex_lock() Mutex locks can be locked 、 Get mutex ;pthread_mutex_unlock() The mutex can be unlocked 、 Release the mutex ;
If the thread doesn't want to be blocked , have access to pthread_mutex_trylock() function
Destroy mutex
pthread_mutex_destroy() Function to destroy the mutex
- You cannot destroy a mutex that has not been unlocked , Otherwise, there will be a mistake ;
- Mutexes that are not initialized cannot be destroyed
Mutex deadlock
// Threads A
pthread_mutex_lock(mutex1);
pthread_mutex_lock(mutex2);
// Threads B
pthread_mutex_lock(mutex2);
pthread_mutex_lock(mutex1);Mutex properties
pthread_mutex_init() When the function initializes the mutex, you can set the properties of the mutex
pthread_mutexattr_destroy() Destroy it
pthread_mutexattr_gettype() Function to get the mutex type attribute ,
pthread_mutexattr_settype() modify / Set the mutex type property ,
pthread_mutex_t mutex;
pthread_mutexattr_t attr;
/* Initialize mutex property object */
pthread_mutexattr_init(&attr);
/* Set the type property to PTHREAD_MUTEX_NORMAL */
pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_NORMAL);
/* Initialize mutex */
pthread_mutex_init(&mutex, &attr);
......
/* After use */
pthread_mutexattr_destroy(&attr);
pthread_mutex_destroy(&mutex);Interview questions : In turn, print 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);
}边栏推荐
- Reflection - project management thinking of small and medium-sized companies - make the products first and the functions first
- 语音处理工具:sox
- Target tracking [single target tracking (vot/sot), target detection, pedestrian re identification (re ID)]
- Un voyage profond d'IA dans Huawei Cloud
- Voice annotation automatic segment alignment tool sppas usage notes
- laravel 中 distinct() 的使用方法与去重
- Flutter shared_ Preferences use
- 自动化运维管理平台——spug的搭建和日常使用
- Swift中@dynamicMemberLookup和callAsFunction特性实现对象透明代理功能
- Hook 简介
猜你喜欢

MySQL system keyword summary (official website)

802.11--802.11n协议 PHY

About the many to many cascading insertion of sqlsugar (the ID of the collection attribute cannot be obtained, so the intermediate table cannot be maintained)

Debugging nocturnal simulator with ADB command

友元,静态关键字,静态方法以及对象间的关系

城通网盘仿蓝奏网盘源码 附带视频教程

solidity部署和验证代理合约

Seven common sorts

A review of visual SLAM methods for autonomous driving vehicles

【微服务|OpenFeign】openfeign的超时时间
随机推荐
Matlab 用法
Product security - small vulnerabilities cause big problems
Swift中@dynamicMemberLookup和callAsFunction特性实现对象透明代理功能
Taro 介绍
SizeBalanceTree
Soliciting articles and contributions - building a blog environment with a lightweight application server
Hands on deep learning (I) -- linear neural network
Protobuf binary file learning and parsing
laravel 中 distinct() 的使用方法与去重
Stm32 usart+dma usage based on Hal Library
Hook introduction
Code:: blocks code formatting shortcuts
Seven common sorts
PostgreSQL installation: the database cluster initialization failed, stack hbuilder installation
Should product managers learn from ink knife or Axure?
练习-选择排序
[quantitative investment system] factor processing and installation Talib
Exercise - select sort
开户买基金,通过网上基金开户安全吗?-
一个高频问题,三种模型思维来破解此风控难题