Linux多線程鎖屬性設置方法
互斥鎖是Linux下多線程資源保護的常用手段,但是在時序復雜的情況下,很容易會出現(xiàn)死鎖的情況。
可以通過設置鎖的屬性,避免同一條線程重復上鎖導致死鎖的問題。
通過int pthread_mutexattr_settype(pthread_mutexattr_t *attr, int type)接口設置
一般是以下四種屬性:
PTHREAD_MUTEX_NORMAL This type of mutex does not detect deadlock. A thread attempting to relock this mutex without first unlocking it will deadlock. Attempting to unlock a mutex locked by a different thread results in undefined behaviour. Attempting to unlock an unlocked mutex results in undefined behaviour. PTHREAD_MUTEX_ERRORCHECK This type of mutex provides error checking. A thread attempting to relock this mutex without first unlocking it will return with an error. A thread attempting to unlock a mutex which another thread has locked will return with an error. A thread attempting to unlock an unlocked mutex will return with an error. PTHREAD_MUTEX_RECURSIVE A thread attempting to relock this mutex without first unlocking it will succeed in locking the mutex. The relocking deadlock which can occur with mutexes of type PTHREAD_MUTEX_NORMAL cannot occur with this type of mutex. Multiple locks of this mutex require the same number of unlocks to release the mutex before another thread can acquire the mutex. A thread attempting to unlock a mutex which another thread has locked will return with an error. A thread attempting to unlock an unlocked mutex will return with an error. PTHREAD_MUTEX_DEFAULT Attempting to recursively lock a mutex of this type results in undefined behaviour. Attempting to unlock a mutex of this type which was not locked by the calling thread results in undefined behaviour. Attempting to unlock a mutex of this type which is not locked results in undefined behaviour. An implementation is allowed to map this mutex to one of the other mutex types.
這里主要指同一條線程重復上鎖,不同線程上鎖,無論設置什么屬性,當鎖已經(jīng)被鎖定后都會互斥阻塞。
使用PTHREAD_MUTEX_RECURSIVE屬性,當同一條線程在沒有解鎖的情況下嘗試再次鎖定會返回成功。
以上就是小編為大家?guī)淼腖inux多線程鎖屬性設置方法全部內(nèi)容了,希望大家多多支持腳本之家~
相關文章
ubuntu中python調(diào)用C/C++方法之動態(tài)鏈接庫詳解
這篇文章主要給大家介紹了關于如何在ubuntu中python調(diào)用C/C++方法之動態(tài)鏈接庫的相關資料,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起看看吧2018-11-11Ubuntu18.04(linux)安裝MySQL的方法步驟
本篇文章主要介紹了Ubuntu18.04(linux)安裝MySQL的方法步驟,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2018-05-05Linux下安裝MariaDB數(shù)據(jù)庫問題及解決方法(二進制版本的安裝)
MariaDB數(shù)據(jù)庫 分為源代碼版本和二進制版本,源代碼版本需要cmake編譯,這里是二進制版本的安裝。下面通過本文給大家介紹Linux下安裝MariaDB數(shù)據(jù)庫問題及解決方法(二進制版本的安裝),感興趣的朋友參考下吧2016-11-11Linux系統(tǒng)下Nginx支持ipv6配置的方法
這篇文章主要介紹了Linux系統(tǒng)下Nginx支持ipv6的方法,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2018-12-12在CentOS 7上給一個網(wǎng)卡分配多個IP地址的方法
本篇文章主要介紹了在CentOS 7上給一個網(wǎng)卡分配多個IP地址的方法,具有一定的參考價值,有需要的可以了解一下。2017-03-03Linux內(nèi)核設備驅動之內(nèi)核中鏈表的使用筆記整理
今天小編就為大家分享一篇關于Linux內(nèi)核設備驅動之內(nèi)核中鏈表的使用筆記整理,小編覺得內(nèi)容挺不錯的,現(xiàn)在分享給大家,具有很好的參考價值,需要的朋友一起跟隨小編來看看吧2018-12-12