https://stackoverflow.com/a/189778/462608
In the case of non-recursive mutexes, there is no sense of ownership and any thread can usually release the mutex no matter which thread originally took the mutex.
我对互斥量的研究是,当一个线程想要对共享对象做某事时,它会获取它,当它完成它想做的任何事情时,它就会释放锁。同时其他线程可以休眠或自旋锁。
上面引用的“无论哪个线程最初占用互斥锁,任何线程通常都可以释放互斥锁”是什么意思。?
我错过了什么?
最佳答案
这在不同的线程实现之间可能会有所不同,但是由于您已经用“pthreads”标记了您的问题,我假设您对 pthread 互斥体感兴趣(而不是 vxworks 互斥体,这显然是您提供的链接所描述的)。
因此在 pthreads 中的规则是锁定互斥量的同一个线程必须解锁它。您可以在互斥对象上设置属性,无论您是希望在违反此规则时生成错误,还是结果是未定义的行为(例如,对于调试版本或发布版本)。有关详细信息,请参阅 pthread_mutexattr_settype 函数的联机帮助页。
关于multithreading - 无论哪个线程最初占用互斥锁,任何线程通常如何释放非递归互斥锁?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10548900/