28

The Boost Libraries don't seem to have a device for setting a thread's priority. Would this be the best code to use on Linux or is there a better method?

boost::thread myThread( MyFunction() );

struct sched_param param;
param.sched_priority = 90;
pthread_attr_setschedparam( myThread.native_handle(), SCHED_RR, &param);

I don't have alot of Linux programming experience.

Newton Falls
  • 2,148
  • 3
  • 17
  • 22
SlickMcRunFast
  • 293
  • 1
  • 3
  • 9
  • Arafangion: Do you have anything to back that up? The linux man page for pthread_attr_setschedparam says it does work. Also, my personal experience is that it works exactly as documented. – avl_sweden Nov 18 '11 at 07:10

4 Answers4

27

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.

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.

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.

Be careful with the way you set the priority. Various restrictions apply.

#include <iostream>
#include <boost/thread/thread.hpp>
#include <unistd.h>
#include <sched.h>
#include <cstdio>


void* threadfunc()
{
    sleep(5);
}

void displayAndChange(boost::thread& daThread)
{
    int retcode;
    int policy;

    pthread_t threadID = (pthread_t) daThread.native_handle();

    struct sched_param param;

    if ((retcode = pthread_getschedparam(threadID, &policy, &param)) != 0)
    {
        errno = retcode;
        perror("pthread_getschedparam");
        exit(EXIT_FAILURE);
    }

    std::cout << "INHERITED: ";
    std::cout << "policy=" << ((policy == SCHED_FIFO)  ? "SCHED_FIFO" :
                               (policy == SCHED_RR)    ? "SCHED_RR" :
                               (policy == SCHED_OTHER) ? "SCHED_OTHER" :
                                                         "???")
              << ", priority=" << param.sched_priority << std::endl;


    policy = SCHED_FIFO;
    param.sched_priority = 4;

    if ((retcode = pthread_setschedparam(threadID, policy, &param)) != 0)
    {
        errno = retcode;
        perror("pthread_setschedparam");
        exit(EXIT_FAILURE);
    }

    std::cout << "  CHANGED: ";
    std::cout << "policy=" << ((policy == SCHED_FIFO)  ? "SCHED_FIFO" :
                               (policy == SCHED_RR)    ? "SCHED_RR" :
                               (policy == SCHED_OTHER) ? "SCHED_OTHER" :
                                                          "???")
              << ", priority=" << param.sched_priority << 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(), &param)) == -1)
    {
        perror("sched_getparam");
        exit(EXIT_FAILURE);
    }

    std::cout << " ORIGINAL: ";
    std::cout << "policy=" << ((policy == SCHED_FIFO)  ? "SCHED_FIFO" :
                               (policy == SCHED_RR)    ? "SCHED_RR" :
                               (policy == SCHED_OTHER) ? "SCHED_OTHER" :
                                                          "???")
              << ", priority=" << param.sched_priority << std::endl;


    policy = SCHED_RR;
    param.sched_priority = 2;

    if ((res = sched_setscheduler(getpid(), policy, &param)) == -1)
    {
        perror("sched_setscheduler");
        exit(EXIT_FAILURE);
    }

    boost::thread t1(&threadfunc);

    displayAndChange(t1);

    t1.join();

    return 0;
}
the_summer
  • 439
  • 3
  • 10
Duck
  • 26,924
  • 5
  • 64
  • 92
  • 3
    I was just looking for a quick sample not to go trough manual pages and found this useful. Thanks. –  Aug 12 '11 at 13:05
  • 1
    Compared to `boost::thread` `QThread` does indeed shine in the department of being able to easily set the thread priority. – rbaleksandar Jun 01 '16 at 08:35
3

You can have a look at this library (written by a student on mine, not released yet, look at the svn repository): https://sourceforge.net/projects/threadutility/

Basically, he wrote a wrapper of the boost::thread that allows to specify and set threads' priorities in a portable way, by selecting the right internal implementation depending on the platform (currently Linux or Windows). In Linux, the code uses the sched_setscheduler() syscall.

knulp
  • 170
  • 9
2

boost::thread does have the ability to take in pthread attributes before pthread_create() is called. It provides the type boost::thread::attributes, which itself can only be used to set the stack size (on systems that support it), but the attributes object also presents a .get_native_handle() method, which returns a pthread_attr_t, on which you can set your desired attributes. You can then simply call make_thread() with that boost::thread::attributes object as an argument. See the second and third code boxes in this section: http://www.boost.org/doc/libs/1_53_0/doc/html/thread/thread_management.html#thread.thread_management.tutorial.attributes

Jim Hunziker
  • 14,111
  • 8
  • 58
  • 64
  • So to sum-up, `boost::thread` doesn't provide a way to set the priority and you advise to use the exact workaround that the OP talked about in his question ? – Colin Pitrat Mar 03 '16 at 22:30
  • The difference is whether you set the attributes before or after calling `pthread_create`. Per the docs I linked `boost::thread` explicitly does not allow this save for through the native handles. Here we are getting the native handle on the attributes, so we can set them before there even is a pthread handle. – tiberious726 Mar 03 '16 at 22:37
  • In his question, he says he currently does: `pthread_attr_setschedparam( myThread.native_handle(), SCHED_RR, &param);` So that's what you suggest isn't it ? – Colin Pitrat Mar 03 '16 at 22:39
  • Ah OK, I get the difference, you access the thread handle through the `thread::attibute` before the thread is created. It's not very intuitive ! – Colin Pitrat Mar 03 '16 at 22:44
  • @ColinPitrat yeah, exactly! I stared at the docs for a long time before I realized this was an option... it probably would be clearer if the attribute native handle stuff was mentioned or linked to in the `div` actually called native handle... – tiberious726 Mar 03 '16 at 22:56
1

I don't think Linux really has thread priorities - in most kernels you can use 'Nice' levels, but that is probably about it (Which would simplify the scheduler) - however, not all linux systems will respect that! (Depends on the scheduler).

Arafangion
  • 11,517
  • 1
  • 40
  • 72
  • this answer doesn't correspond with what I have seen or heard about linux. Maybe you're referring to an earlier version? – Alex Jul 18 '13 at 08:01
  • @Alex: What have you heard? It's very likely I'm referring to an earlier version, or a mistaken conception of an earlier version. – Arafangion Jul 18 '13 at 08:03
  • the accepted answer demonstrates setting thread priorities and scheduling under linux so linux does have thread priorities implemented. Regarding the Nice levels I found the documentation: http://git.kernel.org/cgit/linux/kernel/git/next/linux-next.git/tree/Documentation/scheduler/sched-nice-design.txt which talks about the shortcomings (probably the ones you described) and how they have been fixed. – Alex Jul 19 '13 at 14:05