ROS Resources: Documentation | Support | Discussion Forum | Index | Service Status | ros @ Robotics Stack Exchange
Ask Your Question
2

callback models for roscpp and rospy

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

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
0

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

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:

http://wiki.ros.org/roscpp/Overview/C...

edit flag offensive delete link more

Comments

4

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 -0500 )edit

Question Tools

2 followers

Stats

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

Seen: 1,294 times

Last updated: Aug 07 '17