@@ -37,12 +37,33 @@ namespace dynamicgraph
37
37
if (!ros.spinner && createAsyncSpinner)
38
38
{
39
39
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
40
56
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);
41
60
}
42
61
else
43
62
{
44
63
if (!ros.mtSpinner && createMultiThreadedSpinner)
45
64
{
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
46
67
ros.mtSpinner = boost::make_shared<ros::MultiThreadedSpinner>(4 );
47
68
}
48
69
}
0 commit comments