linux多執行緒-----同步物件(互斥量、讀寫鎖、條件變數)的屬性

readyao發表於2016-04-01

執行緒具有屬性,同樣用於執行緒同步的物件也有屬性,主要有互斥量、讀寫鎖和條件變數的屬性。

  1. 互斥量屬性:
 #include <pthread.h>

 int pthread_mutexattr_init(pthread_mutexattr_t *attr);
 int pthread_mutexattr_destroy(pthread_mutexattr_t *attr);

互斥量屬性物件的資料型別為pthread_mutexattr_t,用函式pthread_mutexattr_init用預設的互斥量屬性對該屬性物件進行初始化。
用函式pthread_mutexattr_destroy對該屬性物件進行回收。pthread_mutexattr_destroy函式在linux執行緒中什麼事情也不做。

一般有兩個屬性值得關心:程式共享屬性和型別屬性。但是Linux下只支援型別屬性。

程式共享屬性:

int pthread_mutexattr_getpshared(const pthread_mutexattr_t * restrict attr, int *restrict pshared);
int pthread_mutexattr_setpshared(pthread_mutexattr_t *attr, int pshared);

用來獲取和設定互斥量是否被共享。

The process-shared attribute is set to PTHREAD_PROCESS_SHARED to permit a mutex to be operated upon by any thread that has access to the memory where the mutex is allocated, even if the mutex is allocated in memory that is shared by multiple processes.

If the process-shared attribute is PTHREAD_PROCESS_PRIVATE, the mutex shall only be operated upon by threads created within the same process as the thread that initialized the mutex; if threads of differing processes attempt to operate on such a mutex, the behavior is undefined. The default value of the attribute shall be PTHREAD_PROCESS_PRIVATE.

上面的意思主要是:
如果屬性設定為PTHREAD_PROCESS_SHARED,那麼從多個程式共享的記憶體區域中分配的互斥量就可以用於這些程式的同步。
如果屬性設定為PTHREAD_PROCESS_PRIVATE,那麼互斥量只能用於一個程式中的多個執行緒同步。這個是預設的屬性。

互斥量型別屬性:

int pthread_mutexattr_settype(pthread_mutexattr_t *attr, int kind);
int pthread_mutexattr_gettype(const pthread_mutexattr_t *attr, int *kind);

LinuxThreads supports only one mutex attribute: the mutex kind, which is either PTHREAD_MUTEX_FAST_NP for “fast “mutexes, PTHREAD_MUTEX_RECURSIVE_NP for “recursive” mutexes, or PTHREAD_MUTEX_ERRORCHECK_NP for “error checking” mutexes. As the NP suffix indicates, this is a non-por‐table extension to the POSIX standard and should not be employed in portable programs.

上面的一段話說明linux的執行緒僅僅支援這一種互斥量屬性也就是型別屬性,kind的值可以為後面三個巨集:
PTHREAD_MUTEX_FAST_NP或PTHREAD_MUTEX_RECURSIVE_NP或PTHREAD_MUTEX_ERRORCHECK_NP
字尾NP的意思表明該函式不支援POSIX標準,不能在可移植的程式中使用。
預設的屬性為PTHREAD_MUTEX_FAST_NP。

The mutex kind determines what happens if a thread attempts to lock a mutex it already owns with pthread_mutex_lock(3).
If the mutex is of the “fast” kind, pthread_mutex_lock(3) simply suspends the calling thread forever.
If the mutex is of the “error checking” kind,pthread_mutex_lock(3) returns immediately with the error code EDEADLK.
If the mutex is of the “recursive” kind, the call to pthread_mutex_lock(3) returns immediately with a success return code.
The number of times the thread owning the mutex has locked it is recorded in the mutex. The owning thread must call pthread_mutex_unlock(3) the same number of times before the mutex returns to the unlocked state.

上面的一段話說明,一個執行緒呼叫pthread_mutex_lock獲取已經被佔用的互斥量的時候,當型別屬性為”fast”,該執行緒會被暫停。當型別屬性為”error”,該執行緒立刻返回一個錯誤碼EDEADLK。當屬性為”recursive”,該執行緒會成功返回(但是mutex物件內部會有一個值記錄目前有多少執行緒佔有該互斥量,當所有佔有的執行緒都呼叫pthread_mutex_unlock,該互斥量才處於未被佔有的狀態)。

  1. 讀寫鎖屬性:
int pthread_rwlockattr_destroy(pthread_rwlockattr_t *attr);
   int pthread_rwlockattr_init(pthread_rwlockattr_t *attr);

pthread_rwlockattr_init對讀寫鎖物件屬性進行初始化,pthread_rwlockattr_destroy銷燬該屬性結構。

讀寫鎖僅僅支援程式共享屬性

int pthread_rwlockattr_getpshared(const pthread_rwlockattr_t * restrict attr, int *restrict pshared);
int pthread_rwlockattr_setpshared(pthread_rwlockattr_t *attr, int pshared);

上面兩個函式和互斥量中對應的兩個函式功能是一樣的。

3.條件變數屬性:

int pthread_condattr_init(pthread_condattr_t *attr);
int pthread_condattr_destroy(pthread_condattr_t *attr);

pthread_condattr_init對條件變數屬性初始化,pthread_condattr_destroy銷燬該屬性結構。

條件變數也支援程式共享屬性

int pthread_condattr_getpshared(const pthread_condattr_t *restrict attr, int *restrict pshared);
int pthread_condattr_setpshared(pthread_condattr_t *attr,int pshared);

上面兩個函式和互斥量中對應的兩個函式功能是一樣的。

相關文章