linux 互斥锁销毁_c-销毁锁定的互斥锁时pthread_mutex_destroy的正...

我写了以下最小示例:

#include

#include

#include

#define SUCCESS 0

using namespace std;

int main() {

int res;

pthread_mutex_t t;

pthread_mutex_init(&t, NULL);

res = pthread_mutex_lock(&t);

res = pthread_mutex_destroy(&t);

if (res != SUCCESS)

{

cout << "Failed to delete: " << strerror(res) << " # " << t.__data.__lock << " " << t.__data.__nusers << endl;

}

else

{

cout << "Deleted!"<< endl;

}

res = pthread_mutex_unlock(&t);

cout << res << endl;

pthread_exit(NULL);

return 0;

}

Attempting to destroy a locked mutex or a mutex that is referenced (for example, while being used in a pthread_cond_timedwait() or pthread_cond_wait()) by another thread results in undefined behavior.

因此,可以假设,如果它是同一线程,那就可以了.

奇怪的是,这句话在较旧的版本中已更改,因此不存在,并且该行仅表示

It shall be safe to destroy an initialized mutex that is unlocked. Attempting to destroy a locked mutex results in undefined behavior.

因此,认为此更改是出于某种原因并不是一件容易的事,我只是想确定一下.

我在两个不同的linux系统(ubuntu 13.10和另一个debian 5774)上测试了前面提到的代码,它失败并显示“无法删除:设备或资源繁忙#1 1”,在ideone的平台上它成功了.

ideones的行为仅仅是未定义行为的一个特例吗?还是其他情况有问题?

不幸的是,我找不到专门解决此问题的资源.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值