互斥锁的应用与pthread_mutex_destory的出错

简介:
一、互斥锁的应用
互斥量从本质上说就是一把锁, 提供对共享资源的保护访问。
1. 初始化:
在Linux下, 线程的互斥量数据类型是pthread_mutex_t. 
在使用前, 要对它进行初始化:
  对于静态分配的互斥量, 可以把它设置为PTHREAD_MUTEX_INITIALIZER.
  对于动态分配的互斥量, 在申请内存(malloc)之后, 通过pthread_mutex_init进行初始化, 
  并且在释放内存(free)前需要调用pthread_mutex_destroy.


原型:
int pthread_mutex_init(pthread_mutex_t *restrict mutex, const pthread_mutexattr_t *restric attr);
int pthread_mutex_destroy(pthread_mutex_t *mutex);


头文件: #include
返回值: 成功则返回0, 出错则返回错误编号.
说明: 如果使用默认的属性初始化互斥量, 只需把attr设为NULL. 其他值在以后讲解。


2. 互斥操作:
对共享资源的访问, 要对互斥量进行加锁, 如果互斥量已经上了锁, 调用线程会阻塞, 直到互斥量被解锁. 
在完成了对共享资源的访问后, 要对互斥量进行解锁。
首先说一下加锁函数:
头文件:#include
原型:
int pthread_mutex_lock(pthread_mutex_t *mutex);
int pthread_mutex_trylock(pthread_mutex_t *mutex);
返回值: 成功则返回0, 出错则返回错误编号.
说明: 具体说一下trylock函数, 这个函数是非阻塞调用模式, 
也就是说, 如果互斥量没被锁住, trylock函数将把互斥量加锁, 并获得对共享资源的访问权限;
 如果互斥量被锁住了, trylock函数将不会阻塞等待而直接返回EBUSY, 表示共享资源处于忙状态。


再说一下解锁函数:
头文件:#include
原型: int pthread_mutex_unlock(pthread_mutex_t *mutex);
返回值: 成功则返回0, 出错则返回错误编号.


3. 死锁:
死锁主要发生在有多个依赖锁存在时, 会在一个线程试图以与另一个线程相反顺序锁住互斥量时发生. 
如何避免死锁是使用互斥量应该格外注意的东西。


总体来讲, 有几个不成文的基本原则:
对共享资源操作前一定要获得锁。
完成操作以后一定要释放锁。
尽量短时间地占用锁。
如果有多锁, 如获得顺序是ABC连环扣, 释放顺序也应该是ABC。
线程错误返回时应该释放它所获得的锁。


mutex互斥信号量锁住的不是一个变量,而是阻塞住一段程序。
如果对一个mutex变量testlock, 执行了第一次pthread_mutex_lock(testlock)之后,
在unlock(testlock)之前的这段时间内,如果有其他线程也执行到了pthread_mutex_lock(testlock),
这个线程就会阻塞住,直到之前的线程unlock之后才能执行,
由此,实现同步,也就达到保护临界区资源的目的。


二、互斥量的静态分配与动态分配
1. 静态分配
是将互斥锁初始化为 PTHREAD_MUTEX_INITIALIZER.
如:
pthread_mutex_t static_lock = PTHREAD_MUTEX_INITIALIZER;


它不需要,也不能用pthread_mutex_destroy()销毁锁,否则将出错。
官方的解释是:
Mutex initialization using the PTHREAD_MUTEX_INITIALIZER does not immediately initialize the mutex. 
Instead, on first use, pthread_mutex_lock() or pthread_mutex_trylock() branches into a slow path 
and causes the initialization of the mutex. Because a mutex is not just a simple memory object 
and requires that some resources be allocated by the system, 
an attempt to call pthread_mutex_destroy() or pthread_mutex_unlock() on a mutex 
that has statically initialized using PTHREAD_MUTEX_INITIALER and was not yet locked 
causes an EINVAL error.


When an automatically or dynamically allocated mutex is no longer required, 
it should be destroyed using pthread_mutex_destroy(). 
(It is not necessary to call pthread_mutex_destroy() on a mutex 
that was statically initialized using PTHREAD_MUTEX_INITIALIZER.)


2. 动态分析
int pthread_mutex_init(pthread_mutex_t *restrict mutex, const pthread_mutexattr_t *restric attr);
int pthread_mutex_destroy(pthread_mutex_t *mutex);
它们的成对出现,缺一不可。
.h>.h>.h>
相关文章
|
3月前
|
存储 缓存 安全
C语言进程(第二章,wait,sleep,waitpid,pthread_mutex_lock,pthread_mutex_unlock)
C语言进程(第二章,wait,sleep,waitpid,pthread_mutex_lock,pthread_mutex_unlock)
27 0
|
7月前
|
C++
C++11/14/17中提供的mutex系列区别
C++11/14/17中提供的mutex系列类型如下:
|
9月前
|
Linux 编译器 C语言
互斥锁mutex
互斥锁mutex
47 0
pthread_mutex_unlock()出错
pthread_mutex_unlock()出错
104 0
使用超时加锁:pthread_mutex_timedlock
使用超时加锁:pthread_mutex_timedlock
150 0
Pthread线程使用详解
文中先讲解函数,再运行实例,以及一些注意事项
156 0
|
Linux API
pthread_mutex_init & 互斥锁pthread_mutex_t的使用
pthread_mutex_init l         头文件: #include l         函数原型: int pthread_mutex_init(pthread_mutex_t *restrict mutex,const pthread_mutexattr_t *restrict attr); pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER; l         函数作用: 该函数用于C函数的多线程编程中,互斥锁的初始化。
1819 0
|
C++
【C++ 语言】pthread_mutex_t 互斥锁
【C++ 语言】pthread_mutex_t 互斥锁
232 0