Short answer: no, there is not a memory race (in terms of two threads writing to the same memory address at the same time). By default, ROS doesn't start any threads when you make subscribers/callbacks. When you call ros::spin()
or ros::spinOnce()
, it will call each of the subscribers you have added sequentially, in the same thread as the spin
.
Note about your code: if the while(ros::ok()) _effort = this->getEffort();
is running in the main loop, then your subscribers will never get called, since (as mentioned above), in a single threaded program, subscribers are only called when ros::spin()
or ros::spinOnce()
is called. A slight modification to your above code that would call the subscriber would be:
while(ros::ok()) {
ros::spinOnce();
_effort = this->getEffort();
}
Now if you are creating a different thread to run the while(ros::ok()) _effort = this->getEffort();
, then yes, there will be a data race, as ROS doesn't handle memory locks for you. For some more resources on this, look around ROS answers some more (here's one).
Source: mostly from http://wiki.ros.org/roscpp/Overview/C... .