Mutex Attributes

Concept

訂製 mutex 的藍圖

Function

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

Process-Shared Attribute

PTHREAD_PROCESS_SHARED

讓其他 process 也能使用這個 mutex

只有當 mutex 存在 share memory 其他 process 才 access 的到

PTHREAD_PROCESS_PRIVATE (private)

只有此 process 的 thread 能夠使用這個 mutex


Type Attribute

Mutex typeRelock without unlock?Unlock when not owned?Unlock when unlocked?
PTHREAD_MUTEX_NORMALdeadlockundefinedundefined
PTHREAD_MUTEX_ERRORCHECKreturns errorreturns errorreturns error
PTHREAD_MUTEX_RECURSIVEallowedreturns errorreturns error
PTHREAD_MUTEX_DEFAULTundefinedundefinedundefined
  • normal 在重鎖已經被自己鎖住 mutex 時會 deadlock 因為自己變成 blocking 永遠不可能放掉 mutex
  • recursive 會有 lock count 紀錄被重鎖了幾次