-1

Creating thread with priority of '-15' using pthread_setschedparam API. But failing with value EINVAL (as per MAN page : policy is not a recognized policy, or param does not make sense for the policy.) N

As I know priority of process can be renice to value (-19 to 20), But I don't know what is priority range for threads in process. Need expert help to understand this point.

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;
}


Getting error like below:

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]

Thomas Jager
  • 4,836
  • 2
  • 16
  • 30
harishaj90
  • 132
  • 1
  • 10

2 Answers2

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.


Note that 0 is not specified in the list of valid policies.

eerorika
  • 232,697
  • 12
  • 197
  • 326
0

Not everything in eeorika's answer is correct. The documentation for sched_setscheduler, says (amongst other things) this:

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).

and scheduling policy 0 corresponds to SCHED_OTHER, which is why your call is failing.

I may be wrong, but there doesn't actually seem to be a way to raise the priority of an individual thread without root privileges. See also How to increase thread priority in pthreads?.

Paul Sanders
  • 24,133
  • 4
  • 26
  • 48