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

Revision history [back]

click to hide/show revision 1
initial version

ROS can handle the threads for you.
http://wiki.ros.org/roscpp/Overview/Callbacks%20and%20Spinning

ROS can handle the threads for you.
http://wiki.ros.org/roscpp/Overview/Callbacks%20and%20Spinning

When a subscriber goes out of scope, the subscription is canceled. See the fix below. This will service the callbacks with two threads.

void chatterCallback(const std_msgs::String::ConstPtr& msg)
{
 ROS_INFO("I heard: [%s]", msg->data.c_str());
}

int main(int argc, char **argv)
{
 ros::init(argc, argv, "example");

 ros::NodeHandle n;
 ros::Subscriber sub1 = n.subscribe("msg1", 1000, chatterCallback);
 ros::Subscriber sub2 = n.subscribe("msg2", 1000, chatterCallback);

 ros::MultiThreadedSpinner spinner(2);
 spinner.spin();

 return 0;
}