我在 Linux 上使用带有 boost 的 C++。
根据工作类型确定线程优先级的最佳方法是什么? 是否可以在 POSIX/pthread/Linux 中改变线程优先级?
最佳答案
据我所知,boost 不提供执行此操作的 API(C++11 标准肯定至少不提供)。
在 Linux 上,您可以 nice
或 setpriority
每个线程独立于其他线程。但是请注意,这不符合 POSIX 标准:
According to POSIX, the nice value is a per-process setting. However, under the current Linux/NPTL implementation of POSIX threads, the nice value is a per-thread attribute: different threads in the same process can have different nice values. Portable applications should avoid relying on the Linux behavior, which may be made standards conformant in the future.
因为 Linux 使用 pthreads
(boost
的 Linux 端口也是如此)你也可以使用 pthread_setschedparam
它的优点是比 Linux 特定的每线程 nice
行为更具可移植性。
在这两种情况下,由于必须求助于系统设施以管理所谓的“不透明”boost
(或 C++11 中的 std
),因此存在轻微的不安) 资源,因此请谨慎行事(与任何特定于实现的事物一样)。
关于C++ & boost::threads - 如何根据工作类型确定线程的优先级?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19611457/