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

Calling ros::spin from a new Thread

asked 2012-11-05 04:51:03 -0500

mikemodanoxxx gravatar image

updated 2012-11-05 04:55:58 -0500

Lorenz gravatar image

Hi there,

i have a strange problem with ROS. I am trying to subscribe to a ros topic in my application. Since the spin-method blocks the application i just started a new thread and tried to subscribe to the topic within this new thread. Here is my code:

void MatlabDevice::startROSCommunication() {
    boost::thread t(&MatlabDevice::startROSThread, this);

void MatlabDevice::startROSThread() {
    ros::NodeHandle nodeH;
    nodeH.subscribe("chatter", 1000, &MatlabDevice::callbackBodyMessage, this);

void MatlabDevice::callbackBodyMessage(const biorob_msgs::BioBipedBody::ConstPtr &msg) {
   // Further code here

The strange thing is that this worked some time ago. Now i get the following error:

[ERROR] [1352133007.566663012]: SingleThreadedSpinner: You've attempted to call spin from multiple threads.  Use a MultiThreadedSpinner instead.

I tried to remove the spin() call and used the MultiThreadedSpinner but this gives me:

[ERROR] [1352133281.107900251]: MultiThreadeSpinner: You've attempted to call ros::spin from multiple threads... but this spinner is already multithreaded.

Can someone tell me whats wrong with my code?

Greetings, Simon.

edit retag flag offensive close merge delete

2 Answers

Sort by ยป oldest newest most voted

answered 2012-11-05 12:03:50 -0500

tfoote gravatar image

You should only call spin from a single thread. You might now have two instances of this class or another class which now follows the same patter. If you want to do this It's suggested to use the multithreaded Spinner. See the overview page for threading.

edit flag offensive delete link more

answered 2012-11-05 04:59:42 -0500

dornhege gravatar image

updated 2012-11-05 05:00:03 -0500

I guess you are starting the MultiThreadedSpinner from within your threads. The threading should already be done by the spinner.

In addition you are not storing the subscriber anywhere, so I suspect that the anonymously returned object will go out of scope and thus there will be no subscription.

edit flag offensive delete link more


What i forgot to mention is, that another part of the application uses the orocos_ros_integration which is doing some ros interaction. In addition to that i want to use a normal subscriber. Is that even possible? I think my code worked before I integrated it into the code with the orocos integration

mikemodanoxxx gravatar image mikemodanoxxx  ( 2012-11-05 22:06:37 -0500 )edit

Question Tools


Asked: 2012-11-05 04:51:03 -0500

Seen: 4,233 times

Last updated: Nov 05 '12