@@ -37,12 +37,33 @@ namespace dynamicgraph
3737 if (!ros.spinner && createAsyncSpinner)
3838 {
3939 ros.spinner = boost::make_shared<ros::AsyncSpinner> (4 );
40+
41+ // Change the thread's scheduler from real-time to normal and reduce its priority
42+ int oldThreadPolicy, newThreadPolicy;
43+ struct sched_param oldThreadParam, newThreadParam;
44+ if (pthread_getschedparam (pthread_self (), &oldThreadPolicy, &oldThreadParam) == 0 )
45+ {
46+ newThreadPolicy = SCHED_OTHER;
47+ newThreadParam = oldThreadParam;
48+ newThreadParam.sched_priority -= 5 ; // Arbitrary number, TODO: choose via param/file?
49+ if (newThreadParam.sched_priority < sched_get_priority_min (newThreadPolicy))
50+ newThreadParam.sched_priority = sched_get_priority_min (newThreadPolicy);
51+
52+ pthread_setschedparam (pthread_self (), newThreadPolicy, &newThreadParam);
53+ }
54+
55+ // AsyncSpinners are created with the reduced priority
4056 ros.spinner ->start ();
57+
58+ // Switch the priority of the parent thread (this thread) back to real-time.
59+ pthread_setschedparam (pthread_self (), oldThreadPolicy, &oldThreadParam);
4160 }
4261 else
4362 {
4463 if (!ros.mtSpinner && createMultiThreadedSpinner)
4564 {
65+ // Seems not to be used.
66+ // If we need to reduce its threads priority, it needs to be done before calling the MultiThreadedSpinner::spin() method
4667 ros.mtSpinner = boost::make_shared<ros::MultiThreadedSpinner>(4 );
4768 }
4869 }
0 commit comments