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]