Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>That's the basic template for how I would do it but after searching around I found next to no code examples so I guess the verdict is out on whether it is best or not.</p> <p>The problem is that boost::thread does not have a constructor that allows pthead attributes to be passed in at thread creation so you have to make changes after the thread starts. The only other way I know to get around that is through the process/thread schedule inheritance. Unless directed otherwise, new threads will inherit the schedule/priority of their creator so you could change the current thread before creating worker threads and then change back if you want. Seems awkward but it is an alternative.</p> <p>Here's a hack of an example that hopefully demostrates both. You may need to change policy and priority as appropriate and run as root.</p> <p>Be careful with the way you set the priority. Various restrictions apply.</p> <pre><code>#include &lt;iostream&gt; #include &lt;boost/thread/thread.hpp&gt; #include &lt;unistd.h&gt; #include &lt;sched.h&gt; #include &lt;cstdio&gt; void* threadfunc() { sleep(5); } void displayAndChange(boost::thread&amp; daThread) { int retcode; int policy; pthread_t threadID = (pthread_t) daThread.native_handle(); struct sched_param param; if ((retcode = pthread_getschedparam(threadID, &amp;policy, &amp;param)) != 0) { errno = retcode; perror("pthread_getschedparam"); exit(EXIT_FAILURE); } std::cout &lt;&lt; "INHERITED: "; std::cout &lt;&lt; "policy=" &lt;&lt; ((policy == SCHED_FIFO) ? "SCHED_FIFO" : (policy == SCHED_RR) ? "SCHED_RR" : (policy == SCHED_OTHER) ? "SCHED_OTHER" : "???") &lt;&lt; ", priority=" &lt;&lt; param.sched_priority &lt;&lt; std::endl; policy = SCHED_FIFO; param.sched_priority = 4; if ((retcode = pthread_setschedparam(threadID, policy, &amp;param)) != 0) { errno = retcode; perror("pthread_setschedparam"); exit(EXIT_FAILURE); } std::cout &lt;&lt; " CHANGED: "; std::cout &lt;&lt; "policy=" &lt;&lt; ((policy == SCHED_FIFO) ? "SCHED_FIFO" : (policy == SCHED_RR) ? "SCHED_RR" : (policy == SCHED_OTHER) ? "SCHED_OTHER" : "???") &lt;&lt; ", priority=" &lt;&lt; param.sched_priority &lt;&lt; std::endl; } int main(int argc, char* argv[]) { int policy, res; struct sched_param param; if ((policy = sched_getscheduler(getpid())) == -1) { perror("sched_getscheduler"); exit(EXIT_FAILURE); } if ((res = sched_getparam(getpid(), &amp;param)) == -1) { perror("sched_getparam"); exit(EXIT_FAILURE); } std::cout &lt;&lt; " ORIGINAL: "; std::cout &lt;&lt; "policy=" &lt;&lt; ((policy == SCHED_FIFO) ? "SCHED_FIFO" : (policy == SCHED_RR) ? "SCHED_RR" : (policy == SCHED_OTHER) ? "SCHED_OTHER" : "???") &lt;&lt; ", priority=" &lt;&lt; param.sched_priority &lt;&lt; std::endl; policy = SCHED_RR; param.sched_priority = 2; if ((res = sched_setscheduler(getpid(), policy, &amp;param)) == -1) { perror("sched_setscheduler"); exit(EXIT_FAILURE); } boost::thread t1(&amp;threadfunc); displayAndChange(t1); t1.join(); return 0; } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload