进程中线程的最高优先级是多少?

What is highest priority of threads in process?

正在使用 pthread_setschedparam API 创建优先级为“-15”的线程。但因值 EINVAL 而失败(根据 MA​​N 页面:策略不是公认的策略,或者参数对该策略没有意义。)N

据我所知,进程的优先级可以任意设置(-19 到 20),但我不知道进程中线程的优先级范围是多少。需要专家帮助才能理解这一点。

class CRAII_Priority_Thread : public std::thread
{
    std::thread mDSIRecvThread;
    const int   mPolicy;
    sched_param mSchParams;

  public:
    CRAII_Priority_Thread(std::thread&& thr,const int policy, const int priority)
        : mDSIRecvThread{std::move(thr)}
        , mPolicy{policy}
        , mSchParams{priority}
    {
        sched_param currSchParams;
        int currPolicy = 0;
        if (pthread_getschedparam(mDSIRecvThread.native_handle(), &currPolicy, &currSchParams))
        {
            std::cout << "Failed to pthread_getschedparam: ERROR "  << std::strerror(errno) << "\n";
        }
        std::cout << "Current configuration DSIThread ["<< mDSIRecvThread.get_id()
                  << "] currPolicy [" << currPolicy << "] and PRIORITY ["
                  << currSchParams.sched_priority << "]\n";
        std::cout << "Trying to set configuration as DSIThread  ["<< mDSIRecvThread.get_id()
                  << "] currPolicy [" << mPolicy << "] and PRIORITY ["
                  << mSchParams.sched_priority << "]\n";

        int iRet = -1;
        if (iRet = pthread_setschedparam(mDSIRecvThread.native_handle(), mPolicy, &mSchParams))
        {
                switch(iRet)
                {
                        case ESRCH:
                                std::cout << "No thread with the ID thread could be found\n";
                        break;
                        case EINVAL:
                                std::cout << "policy is not a recognized policy, or param does not make sense for the policy.\n";
                        break;
                        case EPERM:
                                std::cout << "The caller does not have appropriate privileges to set the specified scheduling policy and parameters.\n";
                        break;
                        case ENOTSUP:
                                std::cout << "attempt was made to set the policy or scheduling parameters to an unsupported value\n";
                        break;
                        default:
                        break;
                }

            std::cout << "Return value [" << iRet << "] Failed to pthread_setschedparam: ERROR "  << std::strerror(errno) << "\n";
        }
if (pthread_getschedparam(mDSIRecvThread.native_handle(), &currPolicy, &currSchParams))
        {
            std::cout << "Failed to pthread_getschedparam: ERROR "  << std::strerror(errno) << "\n";
        }
        std::cout << "setconfiguration successfull current configuration  DSIThread ["<< mDSIRecvThread.get_id()
                  << "] currPolicy [" << currPolicy << "] and PRIORITY ["
                  << currSchParams.sched_priority << "]\n";
    }

    ~CRAII_Priority_Thread()
    {
        if (mDSIRecvThread.joinable())
        {
            mDSIRecvThread.join();
        }
        else
        {
            std::cout << "ERROR : Failed to join DSI recv thread\n";
        }
    }

  private:
    sched_param sch_params;
};

void thread_function()
{
        std::cout << __FUNCTION__ << "\n";
}
int main()
{
        CRAII_Priority_Thread(std::thread(&thread_function), 0, -15);
        return 0;
}


出现如下错误:

Current configuration DSIThread [140333652039424] currPolicy [0] and PRIORITY [0]

Trying to set configuration as DSIThread [140333652039424] currPolicy [0] and PRIORITY [-15]

policy is not a recognized policy, or param does not make sense for the policy. thread_function

Return value [22] Failed to pthread_setschedparam: ERROR Invalid argument

Failed to pthread_getschedparam: ERROR Invalid argument setconfiguration successfull current configuration DSIThread [140333652039424] currPolicy [0] and PRIORITY [0]

man sched_get_priority_max:

int sched_get_priority_max(int policy);
int sched_get_priority_min(int policy);

sched_get_priority_max() returns the maximum priority value that can be used with the scheduling algorithm identified by policy. sched_get_priority_min() returns the minimum priority value that can be used with the scheduling algorithm identified by policy. Supported policy values are SCHED_FIFO, SCHED_RR, SCHED_OTHER, SCHED_BATCH, SCHED_IDLE, and SCHED_DEADLINE.


请注意,有效策略列表中未指定 0。

eeorika 的回答并非所有内容都是正确的。 sched_setschedulerdocumentation 表示(除其他外):

For threads scheduled under one of the normal scheduling policies (SCHED_OTHER, SCHED_IDLE, SCHED_BATCH), sched_priority is not used in scheduling decisions (it must be specified as 0).

并且调度策略 0 对应于 SCHED_OTHER,这就是您的调用失败的原因。

我可能错了,但实际上似乎没有办法在没有 root 权限的情况下提高单个线程的优先级。另见 How to increase thread priority in pthreads?