有关linux pthread_mutex_unlock()疑问

shinerise 2016-04-25 10:37:00
想问大侠一个疑问,linux pthread的pthread_mutex_unlock()如果解锁一个未加锁的mutex互斥量,手册说行为是未知的。可未知行为是什么呢?系统崩溃?抛出异常?还是没任何效果?

有知道的大侠帮忙解答一下哈,多谢多谢。
...全文
540 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
赵不懂 2016-04-27
  • 打赏
  • 举报
回复
看互斥量设置的属性,属性表贴上去了
renwotao2009 2016-04-26
  • 打赏
  • 举报
回复
编译试试,很大可能没有任何反应
LubinLew 2016-04-26
  • 打赏
  • 举报
回复
只有在属性是 PTHREAD_MUTEX_DEFAULT 才会有未定义行为,其他情况下明确说明会返回error, 具体还需要你在实际平台上测试。 不过研究某个平台的未定义行为是没有意义的,可能会导致代码不可移植等问题,简单了解一下即可, 没有人会故意使用解锁未加锁的方法实现什么功能,你要是不确认有没有锁可以使用函数: pthread_mutex_trylock
LubinLew 2016-04-26
  • 打赏
  • 举报
回复
未知的行为说明具体行为是由实现方决定,POSIX规范不做统一定义。 但是你用man命名查询函数的时候,一般都会告诉你产生什么行为。 The pthread_mutex_unlock() function shall release the mutex object referenced by mutex. The manner in which a mutex is released is dependent upon the mutex’s type attribute. 我的man里面显示解锁一个已经释放的锁产生的行为与mutex的type属性有关, 那你再查属性。 If the mutex type is PTHREAD_MUTEX_NORMAL, deadlock detection shall not be provided. Attempting to relock the mutex causes deadlock. If a thread attempts to unlock a mutex that it has not locked or a mutex which is unlocked, undefined behavior results. If the mutex type is PTHREAD_MUTEX_ERRORCHECK, then error checking shall be provided. If a thread attempts to relock a mutex that it has already locked, an error shall be returned. If a thread attempts to unlock a mutex that it has not locked or a mutex which is unlocked, an error shall be returned. If the mutex type is PTHREAD_MUTEX_RECURSIVE, then the mutex shall maintain the concept of a lock count. When a thread successfully acquires a mutex for the first time, the lock count shall be set to one. Every time a thread relocks this mutex, the lock count shall be incremented by one. Each time the thread unlocks the mutex, the lock count shall be decremented by one. When the lock count reaches zero, the mutex shall become available for other threads to acquire. If a thread attempts to unlock a mutex that it has not locked or a mutex which is unlocked, an error shall be returned. If the mutex type is PTHREAD_MUTEX_DEFAULT, attempting to recursively lock the mutex results in unde- fined behavior. 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.
bigmouthba 2016-04-25
  • 打赏
  • 举报
回复
马克一下,明天自己试试~

23,125

社区成员

发帖
与我相关
我的任务
社区描述
Linux/Unix社区 应用程序开发区
社区管理员
  • 应用程序开发区社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧