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:
//..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, ¶m);
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, ¶m);
- - - - - - - - - - - - - - - - - - - - - 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