13

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?

Mat
  • 202,337
  • 40
  • 393
  • 406
eat_a_lemon
  • 3,158
  • 11
  • 34
  • 50
  • @juxtapose - I removed the greeting from the question to make more room in the question preview for your summary. – jschmier Mar 21 '11 at 15:26

4 Answers4

6

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.

Alnitak
  • 334,560
  • 70
  • 407
  • 495
  • I thought context switching between threads was arbitrary except with respect to its priority weight. I know it is not a lock issue since I do see it switch when it is suppose to...but it does not stay on the background thread as long as the main thread....or it does not stay on the background thread as much as I would like it to heh. – eat_a_lemon Mar 21 '11 at 07:50
  • Yes it is not a priority issue. I had other problems going on. I used a tool called valgrind for thread debugging and memory leak detection. (valgrind.org) It is an awesome tool. – eat_a_lemon Mar 21 '11 at 18:02
  • 3
    This is not an answer. There are cases where you want to reduce the priority of a thread to prevent starvation of another. For example, if you are using GPU compute, and have CPU compute, you want the CPU compute thread to have slightly lower priority to increase GPU utilization. – doug65536 May 31 '13 at 02:16
3

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.

cordellcp3
  • 3,557
  • 1
  • 17
  • 14
  • 4
    I did have a look at it. It is completely vague about the value that you use for the priority. This answer helps but it is missing the part where you reduce the priority. How do I reduce the priority of a thread? What is the "normal" priority? 0? 20? What is "lower" priority? -10? 10? – doug65536 May 31 '13 at 02:20
1

You should have a look at the pthread_attr_t struct. It's passed as a parameter to the pthread_create function. It's used to change the thread attributes and can help you to solve your problem.

If you can't solve it you will have to use a mutex to prevent your main thread to access your buffer before your other thread swaps it.

Opera
  • 983
  • 1
  • 6
  • 17
1

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).

maverik
  • 5,508
  • 3
  • 35
  • 55