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

A rospy subscriber callback thread locking question

asked 2014-01-06 14:19:14 -0500

Pi Robot gravatar image

I have a rospy node that has both a main loop and a subscriber callback that both need to modify the same global variable and I'm wondering if I have to use thread locking to prevent weirdness. The following snippet exhibits what I mean. Does the variable self.move_cmd.angular.z need locking in this situation?

def __init__():

    rospy.subscriber('roi', RegionOfInterest, self.roi_callback)

    while not rospy.is_shutdown():
        if not target_visible:
            self.move_cmd.angular.z = 0


def roi_callback(self, msg):
    if msg.width !=0 and msg.height != 0:
       self.move_cmd.angular.z = 0.1 * msg.x_offset


edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted

answered 2014-01-07 10:45:58 -0500

ahendrix gravatar image

Since callbacks in rospy are processed in a separate thread, I'm pretty sure you need locking for a case like this.

edit flag offensive delete link more


Thanks Austin. I'll lock it up.

Pi Robot gravatar image Pi Robot  ( 2014-01-07 13:17:38 -0500 )edit

Question Tools

1 follower


Asked: 2014-01-06 14:19:14 -0500

Seen: 2,870 times

Last updated: Jan 07 '14