Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- 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 foundn";
- 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 valuen";
- 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 threadn";
- }
- }
- 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;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement