The man page for nice
says "nice() adds inc to the nice value for the calling process. So, can we use it to change the nice value for a thread created by pthread_create
?
nice的手册页说“nice()”将inc添加到调用进程的nice值。那么,我们可以用它来改变pthread_create创建的线程的nice值吗?
EDIT: It seems that we do can set the nice value per thread.
编辑:我们似乎可以为每个线程设置好的值。
I wrote a application ,setting different nice value for different thread, and observed that the "nicer" thread has been scheduled with lower priority. Checking the output, I found that the string "high priority ................" get outputted more frequently.
我写了一个应用程序,为不同的线程设置了不同的好价值,并观察到“更好”的线程已被安排在较低的优先级。检查输出,我发现字符串“高优先级................”更频繁地输出。
void * thread_function1(void *arg)
{
pid_t tid = syscall(SYS_gettid);
int ret = setpriority(PRIO_PROCESS, tid, -10);
printf("tid of high priority thread %d , %d\n",tid ,getpriority(PRIO_PROCESS,tid));
while(1){
printf("high priority ................\n");
}
}
void * thread_function(void *arg)
{
pid_t tid = syscall(SYS_gettid);
int ret = setpriority(PRIO_PROCESS, tid, 10);
printf("tid of low priority thread %d , %d \n",tid ,getpriority(PRIO_PROCESS,tid));
while(1)
{
printf("lower priority\n");
}
}
int main()
{
pthread_t id1;
pthread_t id2;
pid_t pid = getpid();
pid_t tid = syscall(SYS_gettid);
printf("main thread : pid = %d , tid = %d \n" , pid, tid);
pthread_create(&id1, NULL, thread_function1, NULL);
pthread_create(&id2, NULL,thread_function, NULL);
pthread_join(id1, NULL);
pthread_join(id2, NULL);
}
1 个解决方案
#1
22
The pthreads man page says:
pthreads手册页说:
POSIX.1 also requires that threads share a range of other attributes (i.e., these attributes are process-wide rather than per-thread):
POSIX.1还要求线程共享一系列其他属性(即,这些属性是进程范围而不是每个线程):
[...]
[...]
- nice value (
setpriority
(2))- 好的价值(setpriority(2))
So, theoretically, the "niceness" value is global to the process and shared by all threads, and you should not be able to set a specific niceness for one or more individual threads.
因此,从理论上讲,“niceness”值对于进程是全局的并且由所有线程共享,并且您不应该为一个或多个单独的线程设置特定的好处。
However, the very same man page also says:
但是,同样的手册页也说:
LinuxThreads
Linux线程
The notable features of this implementation are the following:
此实现的显着特征如下:
[...]
[...]
- Threads do not share a common nice value.
- 线程不共享一个常见的nice值。
NPTL
NPTL
[...]
[...]
NPTL still has a few non-conformances with POSIX.1:
NPTL仍然与POSIX.1有一些不符合:
- Threads do not share a common nice value.
- 线程不共享一个常见的nice值。
So it turns out that both threading implementations on Linux (LinuxThreads and NPTL) actually violate POSIX.1, and you can set a specific niceness for one or more individual threads by passing a tid
to setpriority() on these systems.
因此,事实证明Linux上的线程实现(LinuxThreads和NPTL)实际上都违反了POSIX.1,并且您可以通过在这些系统上将tid传递给setpriority()来为一个或多个单独线程设置特定的好处。
#1
22
The pthreads man page says:
pthreads手册页说:
POSIX.1 also requires that threads share a range of other attributes (i.e., these attributes are process-wide rather than per-thread):
POSIX.1还要求线程共享一系列其他属性(即,这些属性是进程范围而不是每个线程):
[...]
[...]
- nice value (
setpriority
(2))- 好的价值(setpriority(2))
So, theoretically, the "niceness" value is global to the process and shared by all threads, and you should not be able to set a specific niceness for one or more individual threads.
因此,从理论上讲,“niceness”值对于进程是全局的并且由所有线程共享,并且您不应该为一个或多个单独的线程设置特定的好处。
However, the very same man page also says:
但是,同样的手册页也说:
LinuxThreads
Linux线程
The notable features of this implementation are the following:
此实现的显着特征如下:
[...]
[...]
- Threads do not share a common nice value.
- 线程不共享一个常见的nice值。
NPTL
NPTL
[...]
[...]
NPTL still has a few non-conformances with POSIX.1:
NPTL仍然与POSIX.1有一些不符合:
- Threads do not share a common nice value.
- 线程不共享一个常见的nice值。
So it turns out that both threading implementations on Linux (LinuxThreads and NPTL) actually violate POSIX.1, and you can set a specific niceness for one or more individual threads by passing a tid
to setpriority() on these systems.
因此,事实证明Linux上的线程实现(LinuxThreads和NPTL)实际上都违反了POSIX.1,并且您可以通过在这些系统上将tid传递给setpriority()来为一个或多个单独线程设置特定的好处。