linux多线程-----同步对象(互斥量、读写锁、条件变量)的属性

线程具有属性,同样用于线程同步的对象也有属性,主要有互斥量、读写锁和条件变量的属性。

  1. 互斥量属性:
 #include 

 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);

上面两个函数和互斥量中对应的两个函数功能是一样的。

你可能感兴趣的:(每日一题,Linux技术,服务器编程)