Thread Callback Server
Hi,
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 ?
Tanks
Asked by bulgrozer on 2015-08-25 03:52:04 UTC
Answers
For multithreaded callback queues of ROS have a look at this:
http://wiki.ros.org/roscpp/Overview/Callbacks%20and%20Spinning
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.
Edit:
Includes:
#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
my_spinner.start();
Your callbacks onFooReceived
and onBackReceived
can now be executed in parallel, if messages arrive such that this is appropriate...
Asked by Wolf on 2015-08-25 03:57:30 UTC
Comments
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.
Asked by bulgrozer on 2015-08-25 04:21:32 UTC
Comments