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

Calling ROS subscriber callback functions from another function

asked 2017-03-16 23:19:31 -0500

2ROS0 gravatar image


Is it OK (both technically and in terms of good programming practice) to call the callback function of a certain subscriber from within another function?

For example:

void callback(sensor_msgs::Image::ConstPtr & msg)
    /*do stuff*/

void otherFunction()
    sensor_msgs::Image::ConstPtr image_ptr;
    /*do stuff*/

Thank you

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted

answered 2017-03-17 01:24:02 -0500

gvdhoorn gravatar image

This should be ok, provided that you take care of potential concurrency issues that might arise due to your callback queues being driven by any kind of multithreaded spinners. As your callbacks could then be called in parallel, otherFunction(..) might call callback(..) at a point where callback(..) is already being executed by the callbackqueue instance(s) in your node.

If callback(..) performs any operations that must be atomic or has critical sections, you would need to add some mutual exclusion infrastructure to make sure that two calls to callback(..) can be executed in parallel.

edit flag offensive delete link more

Question Tools

1 follower


Asked: 2017-03-16 23:19:31 -0500

Seen: 1,569 times

Last updated: Mar 17 '17