Launching a real-time thread with higher priority than main

Jan 14, 2008 1 Replies

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:



formatting link


//..in the main thread:



pthread_attr_t threadAttr; struct sched_param param; pthread_attr_init(&threadAttr); pthread_attr_setstacksize(&threadAttr, 32768); pthread_attr_setdetachstate(&threadAttr, PTHREAD_CREATE_DETACHED); pthread_create(&worker,&threadAttr,&worker_function,NULL); pthread_attr_destroy(&threadAttr);


//..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", policy,param.sched_priority); //..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


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)

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 scheduler.

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.

JB

Join the Discussion

Have something to add? Share your thoughts — no account required.

Didn't find your answer?

Ask the community — no account required