How to spawn multiple nodes in same C++ process
Hello there,
I want to spawn multiple nodes within the same C++ process. To this end I used boost/thread. All seems to work, except that always one of the nodes is not showing up in 'rosnode list'. This is the code that runs the threads and creates the nodes:
static boost::mutex mutex;
static boost::once_flag cv_thread_flag = BOOST_ONCE_INIT;
void worker0(int argc, char* argv[]) {
mutex.lock();
ros::init(argc, argv, "optical_flow_visualization0");
optical_flow_analyser::visualize::of_visualize vis("asdf", 0);
mutex.unlock();
ros::spin();
}
void worker1(int argc, char* argv[]) {
mutex.lock();
ros::init(argc, argv, "optical_flow_visualization1");
optical_flow_analyser::visualize::of_visualize vis("asdf", 1);
mutex.unlock();
ros::spin();
}
int main(int argc, char* argv[]) {
boost::call_once(cv_thread_flag, &cv::startWindowThread);
boost::thread worker_thread_0(worker0, argc, argv);
boost::thread worker_thread_1(worker1, argc, argv);
worker_thread_0.join();
worker_thread_1.join();
return 0;
}
Sample output of 'rosnode list':
% rosnode list
/gazebo
/optical_flow_visualization1
(...)
The internal workings of the node consist of subscribing to some image_transport topics and displaying them with cv::imshow() in the callback function. As stated above this does indeed work as it should, but the fact that only one of the nodes is found by rosnode tells me that I might be doing something wrong here.
So I wonder: Is this a sane way of spawning multiple nodes? If not: how should I do it instead?
The background of this is that I want to process multiple camera streams from Gazebo, and I'd like to have it so, that I can flexibly spawn processing nodes, depending on how many camera streams there are.