Rosanswers logo

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


Originally posted by Pi Robot on ROS Answers with karma: 4046 on 2014-01-06

Post score: 1


1 Answer 1


Rosanswers logo

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

Originally posted by ahendrix with karma: 47576 on 2014-01-07

This answer was ACCEPTED on the original site

Post score: 3

Original comments

Comment by Pi Robot on 2014-01-07:
Thanks Austin. I'll lock it up.


Your Answer

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