0
$\begingroup$

Rosanswers logo

For example, I have a ROS node that has several callback and timers. Those callbacks and timers need to read or modify the same global variable. Should I use mutex or other locking mechanism to prevent weird competing conditions? In other words, will those callbacks and timers be processed parallelly or sequentially? And will rospy and roscpp behave the same in this kind of situation?

Thanks a lot.


Originally posted by mayuzumi on ROS Answers with karma: 21 on 2017-08-03

Post score: 2

$\endgroup$

1 Answer 1

0
$\begingroup$

Rosanswers logo

If you use a single-threaded spinner you don't need to use mutex because the callbacks will be called sequentially, therefore only one callback will be reading or writing the variable at a time.

You can read more about it here:

http://wiki.ros.org/roscpp/Overview/Callbacks%20and%20Spinning


Originally posted by Higarian with karma: 51 on 2017-08-07

This answer was ACCEPTED on the original site

Post score: 0


Original comments

Comment by Ed Venator on 2017-08-09:
Importantly, rospy has a completely different threading model. Every subscriber and timer in rospy spawns its own thread, and rospy.spin() is jut an infinite loop that lets those threads operate. For that reason, mutexes are very important in rospy when globals or class variables are used.

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.