Ask Your Question
0

Thread Callback Server

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

bulgrozer gravatar image

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

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
0

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

Wolf gravatar image

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

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

http://wiki.ros.org/roscpp/Overview/C...

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...

edit flag offensive delete link more

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.

bulgrozer gravatar imagebulgrozer ( 2015-08-25 04:21:32 -0600 )edit

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools

1 follower

Stats

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

Seen: 180 times

Last updated: Aug 25 '15