一、互斥量的属性
就像线程有属性一样,线程的同步互斥量也有属性,比较重要的是进程共享属性和类型属性。互斥量的属性用pthread_mutexattr_t类型的数据
表示,当然在使用之前必须进行初始化,使用完成之后需要进行销毁:
1)、互斥量初始化
int pthread_mutexattr_init(pthread_mutexattr_t *attr);
2)、互斥量销毁
int pthread_mutexattr_destroy(pthread_mutexattr_t *attr);
二、互斥量的进程共享属性
进程共享属性有两种值:
1)、PTHREAD_PROCESS_PRIVATE,这个是默认值,同一个进程中的多个线程访问同一个同步对象
2)、PTHREAD_PROCESS_SHARED, 这个属性可以使互斥量在多个进程中进行同步,如果互斥量在多进程的共享内存区域,那么具有这个属性的
互斥量可以同步多进程
设置互斥量进程共享属性
int pthread_mutexattr_getpshared(const pthread_mutexattr_t *restrict attr, int *restrict pshared);
int pthread_mutexattr_setpshared(pthread_mutexattr_t *attr, int pshared);
进程共享属性需要检测系统是否支持,可以检测宏_POSIX_THREAD_PROCESS_SHARED
三、互斥量的类型属性
类型属性
互斥量类型 没有解锁时再次加锁 不占用是解锁 已解锁时解锁
PTHREAD_MUTEX_NORMAL 死锁 未定义 未定义
PTHREAD_MUTEX_ERRORCHEK 返回错误 返回错误 返回错误
PTHREAD_MUTEX_RECURSIVE 允许 返回错误 返回错误
PTHREAD_MUTEX_DEFAULT 未定义 未定义 未定义
获取/设置互斥量的类型属性
int pthread_mutexattr_gettype(const pthread_mutexattr_t *restrict attr, int *restrict type);
int pthread_mutexattr_settype(pthread_mutexattr_t *attr, int type);
四、读写锁与条件变量的属性
1、读写锁也有属性,它只有一个进程共享属性
读写锁属性初始化
int pthread_rwlockattr_destroy(pthread_rwlockattr_t *attr);
int pthread_rwlockattr_init(pthread_rwlockattr_t *attr);
设置读写锁进程共享属性
int pthread_rwlockattr_getpshared(const pthread_rwlockattr_t *restrict attr, int *restrict pshared);
int pthread_rwlockattr_setpshared(pthread_rwlockattr_t *attr, int pshared);
2、条件变量也有进程共享属性
条件变量属性初始化
int pthread_condattr_destroy(pthread_condattr_t *attr);
int pthread_condattr_init(pthread_condattr_t *attr);
设置条件变量属性
int pthread_condattr_getpshared(const pthread_condattr_t *restrict attr, int *restrict pshared);
int pthread_condattr_setpshared(pthread_condattr_t *attr, int pshared);
五、手册
PTHREAD_MUTEXATTR_DESTROY(3POSIX Programmer’s ManPTHREAD_MUTEXATTR_DESTROY(3P)
PROLOG
This manual page is part of the POSIX Programmer’s Manual. The Linux implementation of this interface may differ (con-
sult the corresponding Linux manual page for details of Linux behavior), or the interface may not be implemented on
Linux.
//这只是POSIX的手册,Linux对这个接口的实现可能不一样,或者有的根本没有实现这个接口
NAME
pthread_mutexattr_destroy, pthread_mutexattr_init - destroy and initialize the mutex attributes object
//初始化或者销毁互斥量的属性
SYNOPSIS
#include
//头文件
int pthread_mutexattr_destroy(pthread_mutexattr_t *attr);
int pthread_mutexattr_init(pthread_mutexattr_t *attr);
DESCRIPTION
The pthread_mutexattr_destroy() function shall destroy a mutex attributes object; the object becomes, in effect, unini-
tialized. An implementation may cause pthread_mutexattr_destroy() to set the object referenced by attr to an invalid
value. A destroyed attr attributes object can be reinitialized using pthread_mutexattr_init(); the results of otherwise
referencing the object after it has been destroyed are undefined.
//pthread_mutexattr_destroy()会销毁一个互斥量属性,这个属性将变成一个没有被初始化的值。或者有的实现可能导致
//互斥量的属性是一个无效的值。一个已经被销户的互斥量属性可以重新被初始化,使用一个已经被销毁的属性会导致未
//知的结果
The pthread_mutexattr_init() function shall initialize a mutex attributes object attr with the default value for all of
the attributes defined by the implementation.
//pthread_mutexattr_init()初始化一个互斥量的属性
Results are undefined if pthread_mutexattr_init() is called specifying an already initialized attr attributes object.
//如果初始化一个已经被初始化过的属性,那么结果是未知的
After a mutex attributes object has been used to initialize one or more mutexes, any function affecting the attributes
object (including destruction) shall not affect any previously initialized mutexes.
//对于已经初始化的属性,不会影响以前的互斥量
RETURN VALUE
Upon successful completion, pthread_mutexattr_destroy() and pthread_mutexattr_init() shall return zero; otherwise, an
error number shall be returned to indicate the error.
//成功返回0,失败返回错误码
ERRORS
The pthread_mutexattr_destroy() function may fail if:
//pthread_mutexattr_destroy() 在以下情况失败
EINVAL The value specified by attr is invalid.
//属性值是无效的
The pthread_mutexattr_init() function shall fail if:
// pthread_mutexattr_init()会在以下情况失败
ENOMEM Insufficient memory exists to initialize the mutex attributes object.
//没有内存去初始化属性
These functions shall not return an error code of [EINTR].
//不会返回EINTR
PTHREAD_MUTEXATTR_GETPSHAREPOSIX Programmer’s PTHREAD_MUTEXATTR_GETPSHARED(3P)
PROLOG
This manual page is part of the POSIX Programmer’s Manual. The Linux implementation of this interface may differ (con-
sult the corresponding Linux manual page for details of Linux behavior), or the interface may not be implemented on
Linux.
//这只是POSIX的手册,Linux对这个接口的实现可能不一样,或者有的根本没有实现这个接口
NAME
pthread_mutexattr_getpshared, pthread_mutexattr_setpshared - get and set the process-shared attribute
//获取或者设置互斥量的进程共享属性
SYNOPSIS
#include
//头文件
int pthread_mutexattr_getpshared(const pthread_mutexattr_t * restrict attr, int *restrict pshared);
int pthread_mutexattr_setpshared(pthread_mutexattr_t *attr, int pshared);
DESCRIPTION
The pthread_mutexattr_getpshared() function shall obtain the value of the process-shared attribute from the attributes
object referenced by attr. The pthread_mutexattr_setpshared() function shall set the process-shared attribute in an ini-
tialized attributes object referenced by attr.
//pthread_mutexattr_getpshared()会获取到互斥量的进程共享属性,pthread_mutexattr_setpshared()可以设置互斥量的进程
//共享属性
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 multi-
ple 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_PRO-
CESS_PRIVATE.
//进程共享属性是PTHREAD_PROCESS_SHARED,这样会允许互斥量在任何不同的线程之间同步数据,甚至这两个线程不在同一个
//进程里。 如果属性是PTHREAD_PROCESS_PRIVATE,那么互斥量仅仅允许在同一进程范围内同步数据,如果不同进程之间同步数据
//那么结果是未知的。默认的属性是 PTHREAD_PROCESS_PRIVATE
RETURN VALUE
Upon successful completion, pthread_mutexattr_setpshared() shall return zero; otherwise, an error number shall be
returned to indicate the error.
//成功返回0,失败返回错误码
Upon successful completion, pthread_mutexattr_getpshared() shall return zero and store the value of the process-shared
attribute of attr into the object referenced by the pshared parameter. Otherwise, an error number shall be returned to
indicate the error.
//成功返回0,而且互斥量的进程共享属性被保存起来,否则返回错误码
ERRORS
The pthread_mutexattr_getpshared() and pthread_mutexattr_setpshared() functions may fail if:
//在以下情况失败
EINVAL The value specified by attr is invalid.
//属性值是无效的
The pthread_mutexattr_setpshared() function may fail if:
//pthread_mutexattr_setpshared()会在以下情况失败
EINVAL The new value specified for the attribute is outside the range of legal values for that attribute.
//设置的属性值不在规定范围之内
These functions shall not return an error code of [EINTR].
//不会返回EINTR
PTHREAD_MUTEXATTR_GETTYPE(3POSIX Programmer’s ManPTHREAD_MUTEXATTR_GETTYPE(3P)
PROLOG
This manual page is part of the POSIX Programmer’s Manual. The Linux implementation of this interface may differ (con-
sult the corresponding Linux manual page for details of Linux behavior), or the interface may not be implemented on
Linux.
//这只是POSIX的手册,Linux对这个接口的实现可能不一样,或者有的根本没有实现这个接口
NAME
pthread_mutexattr_gettype, pthread_mutexattr_settype - get and set the mutex type attribute
//获取或者设置互斥量的类型
SYNOPSIS
#include
//头文件
int pthread_mutexattr_gettype(const pthread_mutexattr_t *restrict attr, int *restrict type);
int pthread_mutexattr_settype(pthread_mutexattr_t *attr, int type);
DESCRIPTION
The pthread_mutexattr_gettype() and pthread_mutexattr_settype() functions, respectively, shall get and set the mutex
type attribute. This attribute is set in the type parameter to these functions. The default value of the type attribute
is PTHREAD_MUTEX_DEFAULT.
// pthread_mutexattr_gettype() and pthread_mutexattr_settype() 获取或者设置互斥量的类型。类型会在参数中设置,默认的
//属性是PTHREAD_MUTEX_DEFAULT.
The type of mutex is contained in the type attribute of the mutex attributes. Valid mutex types include:
//互斥量的类型包含在它的属性中,有效的类型包括:
PTHREAD_MUTEX_NORMAL
This type of mutex does not detect deadlock. A thread attempting to relock this mutex without first unlocking it
shall deadlock. Attempting to unlock a mutex locked by a different thread results in undefined behavior.
Attempting to unlock an unlocked mutex results in undefined behavior.
//这个类型不会检测死锁,当一个线程试图加锁一个还没被解锁的互斥量,那么就可能死锁。试图解锁被其他线程
//锁住的互斥量,结果是未知的。解锁已经被解锁的互斥量结果是未知的
PTHREAD_MUTEX_ERRORCHECK
This type of mutex provides error checking. A thread attempting to relock this mutex without first unlocking it
shall return with an error. A thread attempting to unlock a mutex which another thread has locked shall return
with an error. A thread attempting to unlock an unlocked mutex shall return with an error.
//这个类型会提供错误检测。当一个线程试图加锁一个还没被解锁的互斥量,那么就会返回错误码。试图解锁被其他线程
//锁住的互斥量,返回错误。解锁已经被解锁的互斥量,返回错误
PTHREAD_MUTEX_RECURSIVE
A thread attempting to relock this mutex without first unlocking it shall 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 shall 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 shall return
with an error. A thread attempting to unlock an unlocked mutex shall return with an error.
//重新锁住一个未解锁的互斥量会成功。在正常情况下会造成死锁,但是这种类型的却不会。多次锁住互斥量应该有对应的
//多次解锁操作。试图解锁被其他线程锁住的互斥量,返回错误。解锁已经被解锁的互斥量,返回错误
PTHREAD_MUTEX_DEFAULT
Attempting to recursively lock a mutex of this type results in undefined behavior. Attempting to unlock a mutex
of this type which was not locked by the calling thread results in undefined behavior. Attempting to unlock a
mutex of this type which is not locked results in undefined behavior. An implementation may map this mutex to one
of the other mutex types.
//这种类型下,多次锁住互斥量是未知的结果。试图解锁一个未加锁的互斥量,结果是未知的。试图去解锁一个
//其他线程加锁的互斥量,结果是未知的。有的实现会将这种类型映射到其他类型
RETURN VALUE
Upon successful completion, the pthread_mutexattr_gettype() function shall return zero and store the value of the type
attribute of attr into the object referenced by the type parameter. Otherwise, an error shall be returned to indicate
the error.
//成功返回0, pthread_mutexattr_gettype() 会保存类型值,失败返回错误码
If successful, the pthread_mutexattr_settype() function shall return zero; otherwise, an error number shall be returned
to indicate the error.
//成功返回0,失败返回错误码
ERRORS
The pthread_mutexattr_settype() function shall fail if:
//pthread_mutexattr_settype() 会在以下情况失败
EINVAL The value type is invalid.
//类型值无效
The pthread_mutexattr_gettype() and pthread_mutexattr_settype() functions may fail if:
//pthread_mutexattr_gettype() and pthread_mutexattr_settype() 在以下情况失败
EINVAL The value specified by attr is invalid.
//属性值是无效的
These functions shall not return an error code of [EINTR].
//不会返回EINTR
六、实例
互斥量属性使用
1、程序框架
2、源代码
-
#include "apue.h"
-
-
int main()
-
{
-
char *shm = "myshm";
-
char *shm1 = "myshm1";
-
int shm_id, shm_id1;
-
char *buf;
-
pid_t pid;
-
-
pthread_mutex_t *mutex;
-
pthread_mutexattr_t mutexattr;
-
-
-
//打开共享内存
-
shm_id1 = shm_open(shm1, O_RDWR|O_CREAT, 0644);
-
//调整共享内存大小
-
ftruncate(shm_id1, 100);
-
//映射共享内存,MAP_SHARED属性表明,对共享内存的任何修改都会影响其他进程
-
mutex =(pthread_mutex_t *)mmap(NULL, 100, PROT_READ|PROT_WRITE, MAP_SHARED, shm_id1, 0);
-
-
pthread_mutexattr_init(&mutexattr);
-
#ifdef _POSIX_THREAD_PROCESS_SHARED
-
pthread_mutexattr_setpshared(&mutexattr, PTHREAD_PROCESS_SHARED);
-
#endif
-
pthread_mutex_init(mutex, &mutexattr);
-
-
//打开共享内存
-
shm_id = shm_open(shm, O_RDWR|O_CREAT, 0644);
-
//调整共享内存大小
-
ftruncate(shm_id, 100);
-
//映射共享内存,MAP_SHARED属性表明,对共享内存的任何修改都会影响其他进程
-
buf =(char *)mmap(NULL, 100, PROT_READ|PROT_WRITE, MAP_SHARED, shm_id, 0);
-
-
pid = fork();
-
if(pid==0)
-
{
-
//休眠1s,让父进程先运行
-
sleep(1);
-
printf("I'm child proccess\n");
-
-
pthread_mutex_lock(mutex);
-
//将共享内存内存修改为hello
-
memcpy(buf, "hello", 6);
-
printf("child buf is : %s\n", buf);
-
pthread_mutex_unlock(mutex);
-
}
-
else if(pid>0)
-
{
-
printf("I'm parent proccess\n");
-
-
pthread_mutex_lock(mutex);
-
//修改共享内存到内容,改为world
-
memcpy(buf, "world", 6);
-
sleep(3);
-
printf("parent buf is : %s\n", buf);
-
pthread_mutex_unlock(mutex);
-
}
-
-
pthread_mutexattr_destroy(&mutexattr);
-
pthread_mutex_destroy(mutex);
-
//解除映射
-
munmap(buf, 100);
-
//消除共享内存
-
shm_unlink(shm);
-
//解除映射
-
munmap(mutex, 100);
-
//消除共享内存
-
shm_unlink(shm1);
-
}
阅读(831) | 评论(0) | 转发(0) |