linux 多线程编程-互斥锁问题之tpp.c:63: __pthread_tpp_change_priority failed 问题解决

今天让程序跑了一个多线程,发现线程运行后,报以下错误:
__pthread_tpp_change_priority: Assertion `new_prio == -1 || (new_prio >= fifo_min_prio && new_prio <= fifo_max_prio)’ failed.

定义如下:

pthread_mutex_t MsgMutex ;
pthread_t RecvthreadHandle_t;

pthread_create(&RecvthreadHandle_t, NULL, receive\Data, NULL);
pthread_detach(RecvthreadHandle_t);

线程函数如下:

void receive\Data()
{
pthread_mutex_lock(&MsgMutex);
//应用程序
pthread_mutex_unlock(&MsgMutex);
usleep(200 *1000);
}

发现问题原因:
没有对线程定义的变量MsgMutex 进行初始化

经研究发现解决方法如下:
方法一:

pthread_mutex_t MsgMutex = PTHREAD_MUTEX_INITIALIZER;

方法二:

pthread_mutex_t MsgMutex;
pthread_mutex_init(&MsgMutex , NULL);

附注:
1.、线程锁销毁的方法如下:

pthread_mutex_destroy(&MsgMutex)

2、阻塞方式对线程上锁方式如下:

pthread_mutex_lock(&MsgMutex);

3、非阻塞对线程上锁方式如下:

//没有获取到锁,则返回EBUSY 
int err = pthread_mutex_trylock(&MsgMutex);
if(0 != err) {
    if(EBUSY == err) {
        //The mutex could not be acquired because it was already locked.
    }
}

3、阻塞超时的方式对线程上锁的方式如下:

struct timespec abs_timeout;
abs_timeout.tv_sec = time(NULL) + 1;
abs_timeout.tv_nsec = 0;

//阻塞等待线程上锁1s,超时后返回错误码ETIMEDOUT 
int err = pthread_mutex_timedlock(&MsgMutex, &abs_timeout);
if(0 != err) {
    if(ETIMEDOUT == err) {
        //The mutex could not be locked before the specified timeout expired.
        
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

路过的小熊~

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值