linux 互斥锁销毁_销毁锁定的互斥锁时pthread_mutex_destroy的正确行为是什么

本文探讨了在Linux环境下,尝试销毁一个被锁定或被其他线程引用的互斥锁是否会导致未定义行为的问题。通过实例代码展示在Ubuntu 13.10和Debian上,直接销毁锁定的互斥锁会失败,而在某些平台上可能成功。标准规定,锁定的互斥锁或被其他线程使用的互斥锁销毁会导致未定义行为,需要确保在解锁并确保无其他线程使用后再进行销毁。
摘要由CSDN通过智能技术生成

I wrote the following minimal example:

#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;

}

Also at ideone

On one side as som

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值