Ask Your Question

callback models for roscpp and rospy

asked 2017-08-03 02:23:56 -0600

mayuzumi gravatar image

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.

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted

answered 2017-08-07 08:41:31 -0600

Higarian gravatar image

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:

edit flag offensive delete link more



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.

Ed Venator gravatar image Ed Venator  ( 2017-08-09 23:10:01 -0600 )edit

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools



Asked: 2017-08-03 02:23:56 -0600

Seen: 639 times

Last updated: Aug 07 '17