【发布时间】:2015-06-15 10:06:06
【问题描述】:
static int pthrd_setthread_prio(int thred_prio)
{
int thrd_policy = SCHED_RR;
struct sched_param thr_prio;
int res=0;
thr_prio.sched_priority = thred_prio;
/* Try setting the thread/process priority via pthread */
res = pthread_setschedparam( pthread_self(),
thrd_policy,
(const struct sched_param*) &thr_prio);
//pthread_setschedparam has two return values 0 on successs, >0 on failure
if(res != 0)
{
deb_err("pthread_setschedparam failed withe error %d\n",res);
}
res = pthread_getschedparam( pthread_self(),
&thrd_policy,
&thr_prio);
if(res < 0)
{
deb_err("pthread_getschedparam failed\n");
}
printf("Thread policy %s priority %d process id %ld\n", \
( (thrd_policy == SCHED_FIFO) ? "SCHED_FIFO" :
(thrd_policy == SCHED_RR) ? "SCHED_RR" :
(thrd_policy == SCHED_OTHER) ? "SCHED_OTHER" : "???"), thr_prio.sched_priority, syscall(SYS_gettid));
if ( (thr_prio.sched_priority != thred_prio) || (thrd_policy != SCHED_RR) )
{
deb_err("Thread priority == %d, this should be %d ERROR! using pthread\n",thr_prio.sched_priority ,thred_prio);
res=-1;
}
return (res);
}
上面用于设置线程优先级和创建线程 2 的代码导致我的系统在某个点挂起。
我建立了一个 telnet 会话并使用“ps -ef”命令检查线程 2 的实时优先级并正确查看其设置。但在后期它导致系统挂起。我通过删除此特定功能并为线程分配默认优先级来确认这一点。
如果我在这里遗漏了什么,谁能告诉我?
【问题讨论】:
-
嗨,Olaf,它不依赖于嵌入式设备/系统,而是在嵌入式设备中实现代码。
-
嗨,奥拉夫,感谢您的支持。我尝试增加父线程的线程优先级,它解决了这个问题。在我们的设计中,就像线程 2(子线程)通过每 100 毫秒连续轮询来将数据传输到线程 1(父线程)。两者都应该具有更高的优先级,否则我们最终会出现挂起问题。将两个优先级都提高到最大解决了这个问题。
-
尽管我很想获得这份荣誉,但我相信这不是我要感谢的。由于它显然与嵌入无关,我可能会建议删除“嵌入”标签?只是为了不混淆其他人。
标签: c embedded pthreads posix rtos