更改本地线程的优先级在Android上的C / C ++优先级、线程、Android

2023-09-13 01:05:03 作者:余生请多指教

出奇的晦涩难懂的pthread API,用于线程的优先级不仅outlandishly科钦prehensible,而且它只是没有在Android上正常工作。

Insanely obscure pthread api for thread priority not only outlandishly incomprehensible, but also it just doesn't work on android.

那么,有没有办法对我来说,减少或增加线程的优先级?

So, is there a way for me to reduce or increase a thread's priority?

int currentPolicy;
struct sched_param sched;
status = pthread_getschedparam(pthread_self(), &currentPolicy, &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

sched.sched_priority:0 currentPolicy:0

优先最小值/最大值:0/0

priority min/max:0/0

此外,无论我做什么,pthread_setschedparam总是返回我的一个错误(无效参数)。 在我的code,我需要减少我的处理程序线程的优先级,以确保其他线程使用更多的CPU时间。或者,我可以提高我的其他线程的线程的优先级,我想为更多的CPU相比,我的过程中的其他线程使用。

Also, no matter what I do, pthread_setschedparam always return an error for me (Invalid argument). In my code, I need to reduce priority of my handler thread to make sure that the other thread uses more cpu time. Or, alternatively, I could boost thread priority of my other thread that I want to use as more cpu compared to other threads in my process.

有没有办法为我做到这一点?我用本地C ++ code而已。

Is there any way for me to do that? I use native c++ code only.

编辑:从这个职位看起来像Android的主题是某种形式的轻量级进程,并修改它们的优先级的 setpriority 的应该被使用。它看起来该功能的工作原理(getpriority之前,并表示以后将优先修改)。不过,我没有看到我的应用程序预期的效果。

From this post looks like android's threads are some kind of light weight processes and to modify priorities for them setpriority should be used. It looks that the function works (getpriority before and after indicates that priority was modified). However, I don't see expected effect in my app.

推荐答案

Android的线程是Linux的线程。没有什么特别之处他们。

Android threads are Linux threads. Nothing special about them.

setpriority()改变线程的好的价值,这会影响在其调度线程的优先级。

setpriority() alters the thread's "nice" value, which affects the priority at which the thread is scheduled.

pthread_setschedparam()可以用来改变的策略的为好。

pthread_setschedparam() can be used to change the policy as well.

一般来说,你不能提升优先级或改变政策,而不提升权限( CAP_SYS_NICE )。你可以降低它,或提高其恢复到正常的优先级。

Generally speaking, you can't raise the priority or change its policy without elevated privileges (CAP_SYS_NICE). You can lower it, or raise it back to "normal" priority.

如果您使用亚行外壳PS -t -p 您可以看到分配给每一个线程的优先级。此信息也包含在Dalvik的堆转储。

If you use adb shell ps -t -p you can see the priorities assigned to every thread. This information is also included in Dalvik stack dumps.

Dalvik的使用 setpriority()如果需要临时提高GC线程的优先级(以避免优先级反转)。如果你看一下该位code 约行625你可以看到它。您还可以看到调用 set_sched_policy(),一个Android库函数,通过改变其中的 cgroup中线程处于

Dalvik uses setpriority() to temporarily raise the GC thread's priority if necessary (to avoid priority inversion). If you look at this bit of code around line 625 you can see it. You can also see the call to set_sched_policy(), an Android library function that changes which cgroup the thread is in.