最近在搞linux, 在用到mutex时, 一下没注意这个mutexattr, 在查死锁上浪费了不少时间...
现在做记录怕以后又忘了...
Name
pthread_mutexattr_gettype, pthread_mutexattr_settype - get and set the mutex type attribute
Synopsis
#include <pthread.h
>
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.
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.
If successful, the pthread_mutexattr_settype
() function shall return zero; otherwise, an error number shall be returned to indicate the error.
Errors
The pthread_mutexattr_settype
() function shall fail if:
EINVAL
The value type
is invalid.
The pthread_mutexattr_gettype
() and pthread_mutexattr_settype
() functions may fail if:
分享到:
相关推荐
- 使用`pthread_mutexattr_settype`设置互斥锁的范围。 **获取互斥锁的范围** - 使用`pthread_mutexattr_gettype`获取互斥锁的范围。 **设置互斥锁类型的属性** - 使用`pthread_mutexattr_setprotocol`设置互斥...
- **pthread_mutexattr_settype**: 设置互斥锁的类型。 **获取互斥锁的类型属性** - **pthread_mutexattr_gettype**: 获取互斥锁的类型。 **设置互斥锁属性的协议** - **pthread_mutexattr_setprotocol**: 设置...
- **设置互斥锁类型的属性**: 使用`pthread_mutexattr_settype`函数设置互斥锁类型。 - **获取互斥锁的类型属性**: 使用`pthread_mutexattr_gettype`函数获取互斥锁类型。 - **设置互斥锁属性的协议**: 使用`pthread...
- **pthread_mutexattr_settype()**: 设置互斥锁的类型。 **5.7 获取互斥锁的类型属性** - **pthread_mutexattr_gettype()**: 获取互斥锁的类型。 **5.8 设置互斥锁属性的协议** - **pthread_mutexattr_set...
- 使用`pthread_mutexattr_settype`函数设置互斥锁的类型。 **获取互斥锁的类型属性** - 使用`pthread_mutexattr_gettype`函数获取互斥锁的类型。 **设置互斥锁属性的协议** - 使用`pthread_mutexattr_set...
- **设置互斥锁的范围**:使用`pthread_mutexattr_settype()`函数设置互斥锁的类型。 - **获取互斥锁的范围**:使用`pthread_mutexattr_gettype()`函数获取互斥锁的类型。 - **设置互斥锁类型的属性**:使用`pthread...
- `pthread_mutexattr_settype`函数用于设置互斥锁的类型。 ##### 获取互斥锁的类型属性 - `pthread_mutexattr_gettype`函数用于获取互斥锁的类型。 ##### 设置互斥锁属性的协议 - `pthread_mutexattr_set...
- **设置互斥锁类型的属性**:通过`pthread_mutexattr_settype()`函数设置互斥锁的类型属性。 - **获取互斥锁的类型属性**:使用`pthread_mutexattr_gettype()`函数获取互斥锁的类型属性。 - **设置互斥锁属性的协议...
通过int pthread_mutexattr_settype(pthread_mutexattr_t *attr, int type)接口设置 一般是以下四种属性: PTHREAD_MUTEX_NORMAL This type of mutex does not detect deadlock. A thread attempting to relock ...
为了避免这种情况,可以使用`pthread_mutexattr_settype`函数设置互斥锁属性为`PTHREAD_MUTEX_RECURSIVE`,这样同一线程可以多次加锁并相应地解锁,防止死锁。 3. **正确使用条件变量** 条件变量常用于线程间的...
在Pthread库中,可以使用`pthread_mutexattr_settype`函数,配合`PTHREAD_MUTEX_RECURSIVE`常量来初始化互斥锁。 2. 使用`pthread_cleanup_push`和`pthread_cleanup_pop`管理资源 在多线程环境中,确保资源的正确...
函数pthread_mutexattr_setpshared和函数pthread_mutexattr_settype用来设置互斥锁属性。 七、小结 本文对C语言多线程pthread库相关函数进行了详细的说明,涵盖pthread库的基本概念、pthread_t类型、pthread_...
但在某些情况下,递归锁是必要的,可以使用`pthread_mutexattr_settype`函数设置互斥锁属性为`PTHREAD_MUTEX_RECURSIVE`,允许同一线程多次加锁。 3. **正确使用线程局部存储(TLS)**: 线程局部存储是一种数据...
pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_RECURSIVE); pthread_mutex_init(&lock, &attr); ``` 2. **避免持有锁时调用阻塞操作**:在信号处理函数中,如果已经持有了锁,应避免调用可能阻塞的操作,因为...
在创建互斥锁时,可以使用`pthread_mutexattr_settype`函数来设定属性,将类型设为`PTHREAD_MUTEX_RECURSIVE`,允许同一线程多次加锁。这样可以处理那些需要在同一线程内重复调用加锁操作的复杂情况。 其次,条件...
pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_RECURSIVE); pthread_mutex_init(&mutex, &attr); ``` 2. **正确使用条件变量** - **应用场景**:当多个线程需要等待某个特定条件满足时,可以使用条件变量。...
`之后添加`pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_RECURSIVE);`,以确保同一线程可以安全地多次锁定同一资源。 #### 经验二:理解线程调度策略 Linux的线程调度策略直接影响到程序的执行效率和响应时间...