5

I have a GUI application that's using pthreads to do some heavy background processing.

While the background processing is running the GUI is very unresponsive and I think that this is because it's being starved of CPU time by the background threads.

On Windows you can ::SetThreadPriority(hThread, THREAD_PRIORITY_BELOW_NORMAL) on the background threads and all is well.

However on Linux I'm using pthreads and I cannot find a good alternative.

I've already considered;

  • ::sched_setscheduler(SCHED_FIFO) or ::sched_setscheduler(SCHED_RR) - this isn't viable as it requires root (not nice for my GUI app) - also this will make the GUI thread have way too much CPU; I only want the GUI to be prioritised over the background threads.
  • ::pthread_setschedparam but when using anything other than SCHED_FIFO or SCHED_RR that's not supported (::sched_get_priority_min(SCHED_OTHER) and ::sched_get_priority_max(SCHED_OTHER) both return 0)
  • Have multiple processes and use ::nice. There is too much coupling between the GUI and the background threads to make this viable (and porting so much code to this design is a major amount of work)
  • Use ::setpriority to re-nice the background threads. This does work - but is directly against what the documentation says: PThreads documentation (so could potentially be "fixed" system-wide at a later date)

I'm convinced that this is quite a common pattern for GUI apps so what have I missed?

Marcus.

EDIT: Added ::setpriority to list of options (thanks ZalewaPL)

2
  • 1
    "... too much coupling between the GUI and the background threads ..." I'd start to over think the design, sry. Commented Dec 7, 2012 at 15:56
  • I'm not sure that's helpful though - there's no reason to not have lots of traffic between the UI & a background thread, and it's quite reasonable to not want to port this to cross-process communication. Commented May 2, 2014 at 2:39

1 Answer 1

3

Setting nice value of the background threads to something higher may help.
Refer to this: Nice-Level for pthreads?

Sign up to request clarification or add additional context in comments.

1 Comment

Thankyou, I did consider this but the documentation mentions processes and not threads. Although it does work for threads (I've just tested it) - I'm not overjoyed about relying on something that is be directly against what the docs say (kernel.org/doc/man-pages/online/pages/man7/pthreads.7.html)

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.