0
$\begingroup$

Rosanswers logo

I am using AsyncSpinner on ROS Indigo to receive the data from multiple point cloud sensors simultaneously. Below is the same code:

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

    ros::NodeHandle n;
    ros::Subscriber sub1 = n.subscribe<sensor_msgs::PointCloud2>("point_cloud1", 1, callback1);
    ros::Subscriber sub2 = n.subscribe<sensor_msgs::PointCloud2>("point_cloud2", 1, callback2);
    ros::Subscriber sub3 = n.subscribe<sensor_msgs::PointCloud2>("point_cloud3", 1, callback3);

    int point_cloud_subs_count = 3;
    ros::AsyncSpinner spinner(point_cloud_subs_count);
    spinner.start();

    while (ros::ok()) {
        // todo
        pub.publish(some_data);
        rate.sleep();
    }
    return 0;
}

In the above code, I have three point cloud subscribers. Hence in order to receive the data from them, I have defined 3 threads. My guess is that each thread is allocated for each subscriber. However, I am not sure.

Following are my questions:

  1. How to decide the thread count for AsyncSpinner?
  2. Does using ros::spinOnce() have the same effect?

Originally posted by ravijoshi on ROS Answers with karma: 1744 on 2019-01-28

Post score: 0

$\endgroup$

1 Answer 1

0
$\begingroup$

Rosanswers logo

If you want optimal performance then the thread count you choose will depend on how many cores your computer has and how many other processor intensive tasks you will need to run concurrently to this node. For example if I have an 8 core computer and I'm running a processor intensive lidar driver node and RVIZ displaying large amounts of information, I could choose 6 threads. This would leave one core spare for the driver and RVIZ, this is with the caveat that the OS is ultimately in control of what runs where. But choosing more threads than you have physical cores will start to degrade performance because there will be unnecessary context switching going on.

To be honest, I'm not sure how ros::spinOnce() interacts with the AsyncSpinner, but also think calling it would be completely unnecessary, sleeping a rate object would achieve the same thing.

Regarding which callback is assigned to which thread, the answer is they aren't. The is a thread pool of three threads and the are assigned as messages are received. For example if messages arrive quickly on your point_cloud_1 topic so that one callback is still running when the next message arrives, then another thread will start running the same callback. So you will have two or even three instances of callback1 running in different threads at the same time.


Originally posted by PeteBlackerThe3rd with karma: 9529 on 2019-01-28

This answer was ACCEPTED on the original site

Post score: 2


Original comments

Comment by ravijoshi on 2019-01-31:
Thank you very much. Can you please explain the following a bit more? if I have an 8 core computer and I'm running a processor intensive lidar driver node and RVIZ displaying large amounts of information, I could choose 6 threads. This would leave one core spare for the driver and RVIZ

$\endgroup$

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.