How to return values from callback function
Hi All,
I am using below function in future to know the asynchronus result of the thread but the thing is this callback will have no return type as ros::spin needs to be installed here. I would like to know how to design this function so that return type can be given to below function including ros::spin.
void subscriber :: handlecallback( )
{
ros::Subscriber sub = n.subscribe("chatter", 1000, &Listener::callback, &listener);
ros::Subscriber sub1 = n.subscribe("ABC", 1000, &Listener::callback, &listener);
ros::Subscriber sub1 = n.subscribe("CBA", 1000, &Listener::callback, &listener);
ros::spin();
}
std::future<void> result = std::async(&subscriber::handlecallback, &abc, rand()%20);
try {
std::cout << "Task returned " << result.get() << "\n";// result.get can't have void here.
} catch (const std::exception &ex) {
std::cout << "Task exited with exception: " << ex.what() << "\n";
std::cerr << "Thread exited with exception: " << ex.what() << "\n";
}
Asked by can-43811 on 2017-12-25 10:06:36 UTC
Comments
Can you explain a little more what you're trying to achieve here. Topic callback functions cannot return a value, they are defined as void by ROS. I also don't understand why you are creating more subscribers in a callback function they will be destroyed as soon as the function returns.
Asked by PeteBlackerThe3rd on 2017-12-29 12:01:42 UTC