疯狂地隐藏线程优先级的pthread api不仅难以理解,而且它在android上不起作用。在c/C++中更改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
优先最小值/最大值:0/0
而且,不管我做什么,pthread_setschedparam总是为我返回一个错误(无效的参数)。 在我的代码中,我需要减少我的处理程序线程的优先级,以确保另一个线程使用更多的CPU时间。或者,或者,我可以提高我的另一个线程的线程优先级,我希望将其用作比我的进程中的其他线程多的cpu。
有没有什么办法可以做到这一点?我只使用本机C++代码。
编辑:从这篇文章看起来像android的线程是某种轻量级进程,并修改它们的优先级setpriority应该使用。看起来该函数起作用(getpriority之前和之后表示优先级已被修改)。但是,我在我的应用程序中看不到预期的效果。
“* ... pthread_setschedparam总是为我返回一个错误(无效参数)。*”,那么你介意告诉我们你传递给函数的东西,让它返回'EINVAL'吗? – alk
currentPolicy我试图通过所有可用的值(0,1,2,3)。对于sched.sched_priority,我尝试设置不同的值(-10,10等)。全部失败。 – Pavel