C linux pthread thread priority C linux pthread thread priority multithreading multithreading

C linux pthread thread priority


In my experience, if, in the absence of prioritisation your main thread is getting more CPU then this means one of two things:

  1. it actually needs the extra time, contrary to your expectation, or

  2. the background thread is being starved, perhaps due to lock contention

Changing the priorities will not fix either of those.


have a look at pthread_setschedparam() --> http://www.kernel.org/doc/man-pages/online/pages/man3/pthread_setschedparam.3.html

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

You can set the priority in the sched_priority field of the sched_param.


Use pthread_setschedprio(pthread_t thread, int priority). But as in other cases (setschedparam or when using pthread_attr_t) your process should be started under root, if you want to change priorities (like nice utility).