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

Thread Callback Server

asked 2015-08-25 03:52:04 -0500

bulgrozer gravatar image


This time i try, in a single node, to have a server, a subscriber and a publisher. I receive a request from a client and publish it to other nodes and need the answer to send it back to the client. So I have to "pause" my server callback and wait for the answer from the subscriber callback.

I assume that I need to multithread my Node because the server may not be put in a wait mode without a while (flag!=true) and I know how to do the multithreading with posix but here it is more a problem of queuing and spinner.

My code is simple, when i receive the answer it copy the request into a msg and wait with a while. I have a callback for my subscriber. I've tried to introduce boost::thread and callbackqueue but coudn't find a way to make it work. Do you have tips in order to do that ?


edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted

answered 2015-08-25 03:57:30 -0500

Wolf gravatar image

updated 2015-08-25 05:08:52 -0500

For multithreaded callback queues of ROS have a look at this:

Note that if you uses MultiThreaded or AsynchSpinners your callbacks might be called in parallel, i. e. you will need mutexes and condition_variables to properly sync your callbacks and avoid race conditions.



#include <ros/ros.h>
#include <ros/callback_queue.h>

Set up like this:

// create instances
ros::NodeHandle my_nh;
ros::CallbackQueue my_queue;
ros::AsyncSpinner my_spinner( 2 /*number of threads*/, &my_queue /* spinner exclusively for my_queue */);

// bind the queue to the node handle
my_nh.setCallbackQueue( &my_queue );

// now advertise/subscribe using my_nh
ros::Subscriber my_subscriber_foo = my_nh.subscribe( "foo", 1, onFooReceived );
ros::Subscriber my_subscriber_bar = my_nh.subscribe( "bar", 1, onBarReceived );

// now start the spinner

Your callbacks onFooReceived and onBackReceived can now be executed in parallel, if messages arrive such that this is appropriate...

edit flag offensive delete link more


Yes, I have looked at this page multiple time but I don't understand in case of multi-threaded spinner how you associate a spinner to a queue. In my case I need the subscriber to look at a msg while my service still running.

bulgrozer gravatar image bulgrozer  ( 2015-08-25 04:21:32 -0500 )edit

Question Tools

1 follower


Asked: 2015-08-25 03:52:04 -0500

Seen: 344 times

Last updated: Aug 25 '15