C linux pthread thread priority

eat_a_lemon picture eat_a_lemon · Mar 21, 2011 · Viewed 34.2k times · Source

My program has one background thread that fills and swaps the back buffer of a double buffer implementation. The main thread uses the front buffer to send out data. The problem is the main thread gets more processing time on average when I run the program. I want the opposite behavior since filling the back buffer is a more time consuming process then processing and sending out data to the client.

How can I achieve this with C POSIX pthreads on Linux?

Answer

Alnitak picture Alnitak · Mar 21, 2011

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.