Launching a real-time thread with higher priority than main

Do you have a question? Post it now! No Registration Necessary

Translate This Thread From English to

Threaded View
My application needs a main thread for the user interface and a real-time thread
to monitor some analog inputs.  So this is what I did, based on the article
found here:

// the main thread:

    pthread_attr_t threadAttr;
    struct sched_param param;
  pthread_attr_setstacksize(&threadAttr, 32768);
  pthread_attr_setdetachstate(&threadAttr, PTHREAD_CREATE_DETACHED);

//..Then, in the worker_function:

    struct sched_param param;
    int policy;
    pthread_t thread_id = pthread_self();
  pthread_getschedparam(thread_id, &policy, &param);

  printf("existing policy=%1d, priority=%1d\r\n",
//..This yields policy=SCHED_OTHER, priority=0..

  policy = SCHED_RR;
  param.sched_priority = 90;
  pthread_setschedparam(thread_id, policy, &param);

 - - - - - - - - - - - - - - - - - - - - -
This all seems to work fine.  But I don't know why the article suggested 90 for
the priority.  And I don't know why SCHED_RR (which I assume stands for
round-robin) is the proper policy for my worker thread.  I don't need
exceptionally high priority for my worker thread.  I just need it to be
sllightly higher than my main thread.  So should I do a getschedparam on the
main thread and just add 1 for the worker priority?

Robert Scott
Ypsilanti, Michigan

Re: Launching a real-time thread with higher priority than main
Quoted text here. Click to load it

You can setup realtime priorities from 1 to 99. 1 would be enough if there
are no other realtime processes (but consider the interrupts, see below)

Quoted text here. Click to load it

You must change it to one of the realtime scheduler policies (SCHED_RR or
SCHED_FIFO). That's all. SCHED_OTHER would keep it at the non realtime

Quoted text here. Click to load it

If you only have one realtime thread/process is doesn't matter what priority
it has. If there are more than one of them you must select their priority
very carefully. BTW: Interrupts in RT Preempt are running at realtime
priority 50 as default.


Site Timeline