Equivalent of SetThreadPriority on Linux (pthreads)

前端 未结 5 1643
清歌不尽
清歌不尽 2020-12-23 12:24

Given the following bit of code, I was wondering what the equivalent bit of code would be in linux assuming pthreads or even using the Boost.Thread API.

#inc         


        
5条回答
  •  隐瞒了意图╮
    2020-12-23 13:19

    The POSIX standard includes pthread_setschedparam(3), as mentioned by various other answers. Mostly this POSIX thread library function is mentioned when talking of real-time threads, but the POSIX standard does not limit its use solely to the domain of real-time threads. However, in Linux its use is only really meaningful if using real time scheduling classes SCHED_FIFO or SCHED_RR as only those scheduling classes allow more than one value for the priority parameter. See this stack overflow answer for an illustration.

    Fortunately or unfortunately, it is a matter of perspective, it seems both main stream Linux POSIX thread library implementations (the obsolete LinuxThreads and the current NPTL implementation) are not fully POSIX compliant in that the "nice value" is not process specific but thread specific parameter, so it seems you could use setpriority(3) to change the niceness of a thread in Linux. This claim is based on the compatibility notes in pthreads(7) manual page (search for "nice value" in that page); I have not actually tested in practise (straightforward thing to do).

    Should you decide to use the POSIX incompliant way of changing the thread niceness, note that there is the lurking possibility that somebody decides to fix the mentioned non-compliance, in which case there seems to be no way of changing the thread priority in Linux if using normal scheduling class (SCHED_OTHER).

提交回复
热议问题