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)