4. API 比较细节 |
#include <pthread.h>
int pthread_mutexattr_init (pthread_mutexattr_t *attr)
int pthread_mutexattr_destroy (pthread_mutexattr_t *attr)
int pthread_mutexattr_setpshared (pthread_mutexattr_t *attr, int pshared)
int pthread_mutexattr_getpshared (pthread_mutexattr_t *attr, int *pshared)
pthread_mutexattr_init() 将与这个对象相联系的属性初始化成它们缺省的值。在执行期间,由线索系统分配存储器。pthread_mutexattr_destroy()移走那个存储器,属性对象变得无效。
缺省的属性值为:
pthread_mutexattr_t mattr;
ret = pthread_mutexattr_init (&mattr); /* initialize an attribute to default value */
ret = pthread_mutexattr_destroy (&mattr); /* destroy an attribute */
交互锁变量的范围可以是进程私有或者是系统范围。如果在PTHREAD_SHARE_PROCESS 状态中创建交互锁,而且它存在于共享内存,它可以共享在多于一个进程的线索中。这等价于在mutex_init ()中的USYNC_PROCESS 标识。
pthread_mutexattr_t mattr;
int pshared;
ret = pthread_mutexattr_getpshared (&mattr, &pshared); /* get pshared of mutex */ ret = pthread_mutexattr_setpshared (&mattr, PTHREAD_SHARE_PROCESS);
ret = pthread_mutexattr_setpshared (&mattr, PTHREAD_SHARE_PRIVATE);
Copyright: NPACT |