0
$\begingroup$

Rosanswers logo

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.


Originally posted by nitschej on ROS Answers with karma: 35 on 2011-08-25

Post score: 3

$\endgroup$

2 Answers 2

0
$\begingroup$

Rosanswers logo

I am surprised this seemed to work at all. I would not expect a single process to provide more than one node.

I recommend implementing each thread as a nodelet. They are well-supported and the nodelet manager can dynamically load additional threads for the process when needed.

Many camera drivers provide device nodelets, allowing you to display shared copies of the images published. The image pipeline also provides nodelets for similar reasons.


Originally posted by joq with karma: 25443 on 2011-08-25

This answer was ACCEPTED on the original site

Post score: 5

$\endgroup$
0
$\begingroup$

Rosanswers logo

I think you don't need multiple nodes for your application. The main thing ros::init does is set the name and register the remappings.

You just need multiple topic descriptions and that works without a problem (you can remap topic names independent of the node name if you want).


Originally posted by dornhege with karma: 31395 on 2011-08-25

This answer was NOT ACCEPTED on the original site

Post score: 1


Original comments

Comment by nitschej on 2011-08-25:
I have now looked a bit into remapping. If I get it right it will help me to load the same node with different configurations, but only from the command line or a launch file. Combining this with nodelets will let me do the same from within a C++ process. So I will probably go for a combination.

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.