Skip to content
Advertisement

Alternate to setpriority(PRIO_PROCESS, thread_id, priority)

Given – Thread id of a thread.
Requirement – Set Linux priority of the thread id.
Constraint – Cant use setpriority()

I have tried to use below

pthread_setschedprio(pthread_t thread, int prio);
pthread_setschedparam(pthread_t thread, int policy, const struct sched_param *param);

Both the above APIs use pthread_t as an argument. I am not able to construct (typecast) pthread_t from thread id. I understand converting this is not possible due to different types.

Is there a way to still accomplish this ?

Advertisement

Answer

Some aspects of the pthread_setschedprio interface are available for plain thread IDs with the sched_setparam function (declared in <thread.h>). The sched_setparam manual page says that the process is affected (which is the POSIX-mandated behavior), but on Linux, it’s actually the thread of that ID.

Keep in mind that calling sched_setparam directly may break the behavior expected from PI mutexes and other synchronization primitives because the direct call does not perform the additional bookkeeping performed by the pthread_* functions.

User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement