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

Do topic callbacks and timer callbacks run in the same thread?

asked 2016-07-26 05:41:35 -0500

Dimitri Schachmann gravatar image

updated 2016-07-26 05:41:52 -0500

The title says it all actually. I wonder whether I need to protect my subscription callbacks from my timer callbacks with a mutex:

eventSubscriber = nh.subscribe("my_topic", 1, &MyNodeClass::topicCallback, this);
timer           = nh.createTimer(my_period_, boost::bind(&MyNodeClass::timerCallback, this, _1));

Can topicCallback and timerCallback be executed in parallel?

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
3

answered 2016-07-26 06:44:15 -0500

CodeFinder gravatar image

updated 2016-07-26 07:05:28 -0500

No, topicCallback and timerCallback cannot be executed in parallel, as long as you are using single-threaded spinning. For example, if you are using an AsyncSpinner, callbacks are called from multiple threads and additional care needs to be taken.

Timer (and topic/message) callbacks are serviced when calling spin() or spinOnce() (assuming you are not using some custom CallbackQueue handling).

Refer to http://wiki.ros.org/roscpp/Overview/C... for more details. ;-)

edit flag offensive delete link more

Question Tools

1 follower

Stats

Asked: 2016-07-26 05:41:35 -0500

Seen: 2,364 times

Last updated: Jul 26 '16