`
sabolasi
  • 浏览: 906971 次
文章分类
社区版块
存档分类
最新评论

如果pthread_mutex_unlock解锁的mutex未被锁上 及 其它

 
阅读更多

SYNOPSIS

#include <pthread.h>

int pthread_mutex_unlock(pthread_mutex_t *mutex);


DESCRIPTION

The pthread_mutex_unlock() function attempts to unlock the specified mutex. If there are threads blocked on the mutex object when pthread_mutex_unlock() is called, resulting in the mutex becoming available, the scheduling policy is used to determine which thread acquires the mutex.

If the mutex type is PTHREAD_MUTEX_NORMAL, error detection is not provided. If a thread attempts to unlock a mutex that is has not locked or a mutex which is unlocked, undefined behavior results.

If the mutex type is PTHREAD_MUTEX_ERRORCHECK, error checking is provided. If a thread attempts to unlock a mutex that it has not locked or a mutex that is unlocked, an error is returned.

If the mutex type is PTHREAD_MUTEX_RECURSIVE, the mutex maintains the concept of a lock count. When a thread successfully acquires a mutex for the first time, the lock count is set to one. Every time a thread relocks this mutex, the lock count is incremented by one. When the lock count reaches zero, the mutex becomes available for other threads to acquire. If a thread attempts to unlock a mutex that it has not locked or a mutex that is unlocked, an error is returned.

If the mutex type is PTHREAD_MUTEX_DEFAULT, attempting to unlock the mutex if it was not locked by the calling thread results in undefined behavior. Attempting to unlock the mutex if it is not locked results in undefined behavior.

静态定义:
static pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
The PTHREAD_MUTEX_INITIALIZER macro initializes the static mutex mutex, setting its attributes to default values. This macro should only be used for static mutexes, as no error checking is performed.

动态定义:

pthread_mutex_t mutex;
pthread_mutex_init (&mutex, NULL);

动态定义和静态定义之间的联系:

The pthread_mutex_init() function shall initialize the mutex referenced by mutex with attributes specified by attr. If attr is NULL, the default mutex attributes are used; the effect shall be the same as passing the address of a default mutex attributes object. Upon successful initialization, the state of the mutex becomes initialized and unlocked.

In cases where default mutex attributes are appropriate, the macro PTHREAD_MUTEX_INITIALIZER can be used to initialize mutexes that are statically allocated. The effect shall be equivalent to dynamic initialization by a call to pthread_mutex_init() with parameter attr specified as NULL, except that no error checks are performed.


Reference:

http://www.duangw.net/computer/history/pthread/mutex.html

http://www.ibm.com/developerworks/cn/linux/l-pthred/

http://www.ibm.com/developerworks/cn/linux/sdk/rt/part5/

http://www.ibm.com/developerworks/cn/linux/l-osmig1.html

分享到:
评论

相关推荐

    pthread_mutex_t_is_too_small_for_large_pids_in_aosp_bionic.patch

    32位android中bionic是32位的,其中的mutex只有一半也就是16位能够存储pid,当通过docker运行android时,大概率pid会超过16位的范围,就可能会导致android中mutex死锁,表现为应用卡住黑屏。 [32-bit ABI bugs]...

    pthread_mutex

    linux 同步与互斥 posix 线程互斥 实现 。(此为博客http://blog.csdn.net/shallnet 文章对应源码下载)

    pthread_testcancel pthread_kill pthread_cancel

    pthread_testcancel pthread_kill pthread_cancel 的使用例子

    线程加锁程序,pthread-mutex-t

    pthread_mutex_t mutex; pthread_mutex_init(&mutex,NULL); pthread_mutex_lock(&mutex); pthread_mutex_unlock(&mutex); pthread_mutex_destroy(&mutex);

    Linux C 多线程编程之互斥锁与条件变量实例详解

    int pthread_mutex_init(pthread_mutex_t *restrict mutex, const pthread_mutexattr_t *restric attr); int pthread_mutex_destroy(pthread_mutex_t *mutex); 头文件: 返回值: 成功则返回0, 出错则返回错误编号....

    pthreads-w32-2-8-0-release.zip

    visual studio多线程支持库,无法解析的外部符号 __imp__pthread_create,该符号在函数 _main 中被引用

    pthread_cond_wait() 用法深入分析

    以下是对pthread_cond_wait的用法进行了详细的分析介绍,需要的朋友可以过来参考下

    数码天空破解文件cccam205

    libpthread.so.0 pthread_cond_wait recv connect pthread_create send accept pthread_cond_signal pthread_cond_init pthread_mutex_unlock pthread_mutex_lock pthread_mutex_init _Jv_RegisterClasses close ...

    linux 内核mutex.c 源码分析实例

    linux 内核 mutex.c 源码分析实例,我的实例主要先初始化了一个互斥体m,然后获取互斥体的锁(解锁),紧接着释放互斥体的锁(解锁)。最后释放互斥体。中间通过输出m.count来显示互斥体的状态。

    qt 下进程条

    pthread_mutex_unlock(&count;_mutex); } return (NULL); } int main(int argc,char ** argv) { pthread_t thread_id1,thread_id2; pthread_create(&thread;_id1,NULL,&thread;_test,NULL); pthread_create(&thread;...

    多线程编程——互斥锁使用(Jack_pthread_mutex_test.rar)

    多线程编程:互斥锁使用。 打包文件包含两个文件:c文件源代码、Makefile文件,运行环境在Ubuntu14.04下,使用自带的gcc编译器,同学们只需将文件夹复制到某一目录下之后在终端执行:1.“make”生成“test”可执行...

    Linux下多线程间通信

    pthread_mutex_lock 声明开始用互斥锁上锁,此后的代码直至调用 pthread_mutex_unlock 为止,均被上锁,即同一时间只能被一个线程调用执行。当一个线程执行到 pthread_mutex_lock处时,如果该锁此时被另一个线程使用...

    编译时报 Looking for pthread_create - not found 的解决办法

    编译时报 Looking for pthread_create - not found 的解决办法 linux gcc 编译时报Looking for pthread_create - not found 其解决办法是...

    Linux系统编程之线程同步

    unlock主动解锁函数,同时将阻塞在该锁上的所有线程全部唤醒,至于哪个线程先被唤醒,取决于优先级、调度。默认:先阻塞、先唤醒。 例如:T1 T2 T3 T4 使用一把mutex锁。T1加锁成功,其他线程均阻塞,直至T1解锁。...

    信号pthread_cond_wait

    信号pthread_cond_wait信号pthread_cond_wait信号pthread_cond_wait信号pthread_cond_wait信号pthread_cond_wait

    Windows多线程编程缺少pthread.h文件问题

    pthread资源包,pthread源码和已经编译好的VS2019_x64版本 #include &lt;pthread.h&gt; pthread_t newThread; pthread_attr_t attr; pthread_attr_init(&attr); pthread_attr_setscope(&attr, PTHREAD_SCOPE_PROCESS); ...

    3_pthread_sync_mutex_with_productor_and_consumer.tgz

    linux 同步与互斥 posix 线程同步互斥 消费者生产者 实现 。(此为博客http://blog.csdn.net/shallnet 文章对应源码下载)

    互斥量mutex的简单使用(实例讲解)

    int pthread_mutex_init(pthread_mutex_t *restrict mutex, const pthread_mutex_t *restrict attr); //初始化mutex int pthread_mutex_destroy(pthread_mutex_t *mutex); //如果mutex是动态分配的,则释放内存前...

    pthread_cond_wait详解

    一篇博客内容 pthread_cond_wait详解

    线程编程——使用pthread_join(Jack_pthread_join_test.rar)

    线程编程——使用pthread_join。 打包文件包含两个文件:c文件源代码、Makefile文件,运行环境在Ubuntu14.04下,使用自带的gcc编译器,同学们只需将文件夹复制到某一目录下之后在终端执行:1.“make”生成“test”可...

Global site tag (gtag.js) - Google Analytics