Pav*_*l P 13 c++ android pthreads android-ndk
疯狂地模糊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));
Run Code Online (Sandbox Code Playgroud)
输出:
sched.sched_priority:0 currentPolicy:0
priority min/max:0/0
此外,无论我做什么,pthread_setschedparam总是为我返回一个错误(无效的参数).在我的代码中,我需要减少处理程序线程的优先级,以确保其他线程使用更多的CPU时间.或者,或者,我可以提升我的其他线程的线程优先级,我希望将其用作我的进程中其他线程的cpu.
我有什么方法可以做到这一点吗?我只使用本机c ++代码.
编辑:从这篇文章看起来像android的线程是某种轻量级的过程,并修改它们的优先级应该使用setpriority.它看起来该函数有效(getpriority之前和之后表示优先级已被修改).但是,我在我的应用程序中看不到预期的效果.
这是一个简单的答案:使用int nice(int increment).它在unistd.h中声明
这是Android的函数代码:
int nice(int increment)
{
int priority = getpriority(PRIO_PROCESS, 0);
return setpriority( PRIO_PROCESS, 0, priority+increment);
}
Run Code Online (Sandbox Code Playgroud)
所以,setpriority可以用来设置线程优先级.
我实际上通过使用nice(-10)或在我的代码中得到了预期的差异nice(10)
Android线程是Linux线程.他们没什么特别的.
setpriority() 改变线程的"nice"值,这会影响线程的调度优先级.
pthread_setschedparam()也可以用来改变政策.
一般来说,如果没有提升权限(CAP_SYS_NICE),则无法提高优先级或更改其策略.您可以降低它,或将其提升回"正常"优先级.
如果使用,adb shell ps -t -p您可以看到分配给每个线程的优先级.此信息也包含在Dalvik堆栈转储中.
setpriority()如果需要,Dalvik用于暂时提高GC线程的优先级(以避免优先级倒置).如果你看看625行左右的这段代码,你可以看到它.您还可以看到调用set_sched_policy()Android库函数,该函数可以更改线程所在的cgroup.
| 归档时间: |
|
| 查看次数: |
10781 次 |
| 最近记录: |