dimanche 7 octobre 2018

Set scheduling parameters for a pthread in c++11

I would really like to give the main thread of my program a higher scheduling priority. I tried this with c++11 threads and it fails...

I am using Raspian Stretch

This was for the main thread:

sched_param sch;
int policy;
pthread_getschedparam(pthread_self(), &policy, &sch);
sch.sched_priority = 10;
if (pthread_setschedparam(pthread_self(), SCHED_FIFO, &sch)) {
    std::cout << "Failed to setschedparam: " << std::strerror(errno) << std::endl;
}

And this for a c++11 thread:

std::unique_ptr<std::thread> Stream;
Stream = std::make_unique<std::thread>([&, this]
{
    while (!finished)
    {
        std::this_thread::sleep_for(std::chrono::milliseconds(2));
        /* more code */
    }
});
sched_param sch;
int policy;
pthread_getschedparam(Stream->native_handle(), &policy, &sch);
sch.sched_priority = 20;
if (pthread_setschedparam(Stream->native_handle(), SCHED_OTHER, &sch))
{
    std::cout << "Failed to setschedparam: " << std::strerror(errno) << std::endl;
}

I get Invalid argument and I do not understand why...

Best wishes

Aucun commentaire:

Enregistrer un commentaire