今天让程序跑了一个多线程,发现线程运行后,报以下错误:
__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.
}
}