用于线程优先级的疯狂模糊的 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
优先级最小/最大:0/0
此外,无论我做什么,pthread_setschedparam 总是为我返回一个错误(无效参数)。在我的代码中,我需要降低处理程序线程的优先级,以确保其他线程使用更多的 cpu 时间。或者,与我的进程中的其他线程相比,我可以提高我想用作更多 cpu 的其他线程的线程优先级。
我有什么办法吗?我只使用本机 C++ 代码。
编辑:从这篇文章看来,android 的线程是某种轻量级进程,应该使用setpriority来修改它们的优先级。看起来该函数有效(getpriority before 和 after 表示优先级已被修改)。但是,我在我的应用程序中看不到预期的效果。