简体   繁体   English

使用pthreads时的后台线程(不错,优先级)

[英]Background threads when using pthreads (nice, priority)

I have a GUI application that's using pthreads to do some heavy background processing. 我有一个GUI应用程序,它使用pthreads进行一些繁重的后台处理。

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. 在后台处理正在运行时,GUI非常无响应,我认为这是因为后台线程正在耗尽CPU时间。

On Windows you can ::SetThreadPriority(hThread, THREAD_PRIORITY_BELOW_NORMAL) on the background threads and all is well. 在Windows上你可以在后台线程上设置:: SetThreadPriority(hThread,THREAD_PRIORITY_BELOW_NORMAL),一切都很好。

However on Linux I'm using pthreads and I cannot find a good alternative. 但是在Linux上我使用的是pthreads,我找不到一个好的选择。

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; :: sched_setscheduler(SCHED_FIFO)或:: sched_setscheduler(SCHED_RR) - 这是不可行的,因为它需要root(对我的GUI应用程序不好) - 这也会使GUI线程拥有太多的CPU; I only want the GUI to be prioritised over the background threads. 我只希望GUI优先于后台线程。
  • ::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) :: pthread_setschedparam但是当使用不支持SCHED_FIFO或SCHED_RR以外的任何东西时(:: sched_get_priority_min(SCHED_OTHER)和:: sched_get_priority_max(SCHED_OTHER)都返回0)
  • Have multiple processes and use ::nice. 有多个进程并使用:: 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) GUI和后台线程之间存在太多耦合以使其可行(并且为此设计移植如此多的代码是一项重要工作)
  • Use ::setpriority to re-nice the background threads. 使用:: setpriority重新调整后台线程。 This does work - but is directly against what the documentation says: PThreads documentation (so could potentially be "fixed" system-wide at a later date) 这确实有效 - 但是直接反对文档所说的内容: PThreads文档 (因此可能会在以后的系统中“修复”)

I'm convinced that this is quite a common pattern for GUI apps so what have I missed? 我确信这是GUI应用程序的一种常见模式,所以我错过了什么?

Marcus. 马库斯。

EDIT: Added ::setpriority to list of options (thanks ZalewaPL) 编辑:添加:: setpriority到选项列表(感谢ZalewaPL)

Setting nice value of the background threads to something higher may help. 将后台线程的良好值设置为更高的值可能会有所帮助。
Refer to this: Nice-Level for pthreads? 请参阅: pthreads的Nice-Level?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM