How to Increase Thread Priority in Pthreads

How to increase thread priority in pthreads?

The default Linux scheduling policy is SCHED_OTHER, which have no priority choice but a nice level to tweak inside the policy.

You'll have to change to another scheduling policy using function pthread_setschedparam (see also man sched_setscheduler)

'Normal' scheduling policies: (from sched_setscheduler(2))

   SCHED_OTHER   the standard round-robin time-sharing policy;
SCHED_BATCH for "batch" style execution of processes; and
SCHED_IDLE for running very low priority background jobs.

Real-time scheduling policies:

   SCHED_FIFO    a first-in, first-out policy; and
SCHED_RR a round-robin policy.

In your case maybe you can use SCHED_BATCH as this does not require root privileges.

Warning: wrong usage of real-time scheduling policies may hang your system. That's why you need root privileges to do this kind of operation.

Just to be sure of what your machine is capable of, you can use chrt tool from
util-linux package.
As an example:

$ chrt -m 
SCHED_OTHER min/max priority : 0/0
SCHED_FIFO min/max priority : 1/99
SCHED_RR min/max priority : 1/99
SCHED_BATCH min/max priority : 0/0
SCHED_IDLE min/max priority : 0/0

A way to waste less time (which I often use):

alias batchmake='time chrt --batch 0 make --silent'

While staying with user privileges, this propels the make by 15% (in my case).

Edit: introducing nice, SCHED_BATCH, SCHED_IDLE and chrt tool. For accuracy ! :)

Equivalent of SetThreadPriority on Linux (pthreads)

The equivalent to SetThreadPriority in linux would be pthread_setschedprio(pthread_t thread, int priority).

Check the man page.

EDIT: here's the sample code equivalent:

#include <pthread.h>

int main()
{
pthread_t thId = pthread_self();
pthread_attr_t thAttr;
int policy = 0;
int max_prio_for_policy = 0;

pthread_attr_init(&thAttr);
pthread_attr_getschedpolicy(&thAttr, &policy);
max_prio_for_policy = sched_get_priority_max(policy);


pthread_setschedprio(thId, max_prio_for_policy);
pthread_attr_destroy(&thAttr);

return 0;
}

This sample is for the default scheduling policy which is SCHED_OTHER.

EDIT: thread attribute must be initialized before usage.

What is the relationship between pthread priority and pthread policy?

Documentation for SCHED_RR says that it is the same as SCHED_FIFO except in certain cases when two or more threads have the same static priority.

Documentation for SCHED_FIFO makes it clear that if a thread with higher static priority is ready-to-run but not running, and if one or more threads with lower static priority are running, then one of the lower priority threads will be preempted in favor of the higher priority thread.

would thread 1 run at all [in the SCHED_RR case]?

That depends. What is thread 0 doing? How many CPUs does the system have? If those were the only two threads on a system that had only one CPU, then thread 1 would be allowed to run whenever thread 0 did not want to run.

Generally speaking, when you use static priorities, you want the highest priority threads to do the least amount of work. A high priority thread should spend most of its time waiting for some event. Then when the event happens, the thread should promptly acknowledge it, and then possibly signal a lower-priority thread if some kind of follow-up computation is required.

would thread 1 run at all [in the SCHED_OTHER case]?

As mentioned in my comment, if you're talking about static priorities (i.e., as set by the sched_setattr() system call, then the question is meaningless because threads that are scheduled under the SCHED_OTHER policy are all required to have the same static priority--zero.



Related Topics



Leave a reply



Submit