使用pthread_setschedparam API创建优先级为“ -15”的线程。但是失败并返回值EINVAL(根据MAN页:策略不是公认的策略,或者param对该策略没有意义。)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;
}
出现如下错误:
当前配置DSIThread [140333652039424] currPolicy [0]和 优先级[0]
尝试将配置设置为DSIThread [140333652039424] currPolicy [0]和优先级[-15]
政策不是公认的政策,否则param对于 政策。 thread_function
返回值[22]失败至pthread_setschedparam:ERROR无效 论点
pthread_getschedparam失败:ERROR无效的参数 setconfiguration成功当前配置DSIThread [140333652039424] currPolicy [0]和PRIORITY [0]
答案 0 :(得分:0)
man sched_get_priority_max
:
int sched_get_priority_max(int policy); int sched_get_priority_min(int policy);
sched_get_priority_max()
返回可与策略标识的调度算法一起使用的最大优先级值。sched_get_priority_min()
返回可与策略标识的调度算法一起使用的最小优先级值。受支持的策略值为SCHED_FIFO
,SCHED_RR
,SCHED_OTHER
,SCHED_BATCH
,SCHED_IDLE
和SCHED_DEADLINE
。
请注意,有效策略列表中未指定0。
答案 1 :(得分:0)
并非eeorika的回答中的所有内容都是正确的。 sched_setscheduler
的{{3}}这样说:
对于根据常规调度策略(SCHED_OTHER,SCHED_IDLE,SCHED_BATCH)之一调度的线程,sched_priority不在调度决策中(必须将其指定为0)。
并且调度策略0对应于SCHED_OTHER
,这就是您的呼叫失败的原因。
我可能是错的,但是实际上似乎没有一种方法可以在没有root特权的情况下提高单个线程的优先级。另请参见documentation。