对于线程优先级而言,pthread的API极其晦涩难懂,不仅如此,它在Android上面也无法使用。
那么,有没有一种方法可以降低或提高线程的优先级呢?
int currentPolicy;
struct sched_param sched;
status = pthread_getschedparam(pthread_self(), ¤tPolicy, &sched);
printf("sched.sched_priority:%d currentPolicy:%d", sched.sched_priority, currentPolicy);
printf("priority min/max:%d/%d", sched_get_priority_min(currentPolicy), sched_get_priority_max(currentPolicy));
输出:
sched.sched_priority:0 currentPolicy:0
priority min/max:0/0
无论我做什么,pthread_setschedparam始终会返回一个错误(无效参数)。在我的代码中,我需要降低处理程序线程的优先级,以确保其他线程使用更多CPU时间。或者,作为替代方案,我可以提高我想要用作比进程中其他线程更多CPU的其他线程的线程优先级。
我能否以任何方式实现这一点?我只使用本地c ++代码。
编辑:从此帖子看来,安卓的线程是某种轻量级进程,并且应该使用setpriority修改它们的优先级。它看起来函数是有效的(before和after的getpriority表明已修改优先级)。但是,在我的应用程序中,我没有看到预期的效果。