Advertisement
Guest User

Untitled

a guest
Jul 17th, 2019
62
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.07 KB | None | 0 0
  1. class CRAII_Priority_Thread : public std::thread
  2. {
  3. std::thread mDSIRecvThread;
  4. const int mPolicy;
  5. sched_param mSchParams;
  6.  
  7. public:
  8. CRAII_Priority_Thread(std::thread&& thr,const int policy, const int priority)
  9. : mDSIRecvThread{std::move(thr)}
  10. , mPolicy{policy}
  11. , mSchParams{priority}
  12. {
  13. sched_param currSchParams;
  14. int currPolicy = 0;
  15. if (pthread_getschedparam(mDSIRecvThread.native_handle(), &currPolicy, &currSchParams))
  16. {
  17. std::cout << "Failed to pthread_getschedparam: ERROR " << std::strerror(errno) << "n";
  18. }
  19. std::cout << "Current configuration DSIThread ["<< mDSIRecvThread.get_id()
  20. << "] currPolicy [" << currPolicy << "] and PRIORITY ["
  21. << currSchParams.sched_priority << "]n";
  22. std::cout << "Trying to set configuration as DSIThread ["<< mDSIRecvThread.get_id()
  23. << "] currPolicy [" << mPolicy << "] and PRIORITY ["
  24. << mSchParams.sched_priority << "]n";
  25.  
  26. int iRet = -1;
  27. if (iRet = pthread_setschedparam(mDSIRecvThread.native_handle(), mPolicy, &mSchParams))
  28. {
  29. switch(iRet)
  30. {
  31. case ESRCH:
  32. std::cout << "No thread with the ID thread could be foundn";
  33. break;
  34. case EINVAL:
  35. std::cout << "policy is not a recognized policy, or param does not make sense for the policy.n";
  36. break;
  37. case EPERM:
  38. std::cout << "The caller does not have appropriate privileges to set the specified scheduling policy and parameters.n";
  39. break;
  40. case ENOTSUP:
  41. std::cout << "attempt was made to set the policy or scheduling parameters to an unsupported valuen";
  42. break;
  43. default:
  44. break;
  45. }
  46.  
  47. std::cout << "Return value [" << iRet << "] Failed to pthread_setschedparam: ERROR " << std::strerror(errno) << "n";
  48. }
  49. if (pthread_getschedparam(mDSIRecvThread.native_handle(), &currPolicy, &currSchParams))
  50. {
  51. std::cout << "Failed to pthread_getschedparam: ERROR " << std::strerror(errno) << "n";
  52. }
  53. std::cout << "setconfiguration successfull current configuration DSIThread ["<< mDSIRecvThread.get_id()
  54. << "] currPolicy [" << currPolicy << "] and PRIORITY ["
  55. << currSchParams.sched_priority << "]n";
  56. }
  57.  
  58. ~CRAII_Priority_Thread()
  59. {
  60. if (mDSIRecvThread.joinable())
  61. {
  62. mDSIRecvThread.join();
  63. }
  64. else
  65. {
  66. std::cout << "ERROR : Failed to join DSI recv threadn";
  67. }
  68. }
  69.  
  70. private:
  71. sched_param sch_params;
  72. };
  73.  
  74. void thread_function()
  75. {
  76. std::cout << __FUNCTION__ << "n";
  77. }
  78. int main()
  79. {
  80. CRAII_Priority_Thread(std::thread(&thread_function), 0, -15);
  81. return 0;
  82. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement