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

How to decide the thread count for AsyncSpinner?

asked 2019-01-28 05:14:12 -0600

ravijoshi gravatar image

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?
edit retag flag offensive close merge delete

1 Answer

Sort by » oldest newest most voted
2

answered 2019-01-28 09:22:41 -0600

updated 2019-01-28 09:26:47 -0600

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.

edit flag offensive delete link more

Comments

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

ravijoshi gravatar image ravijoshi  ( 2019-01-30 23:50:01 -0600 )edit

Question Tools

2 followers

Stats

Asked: 2019-01-28 05:14:12 -0600

Seen: 517 times

Last updated: Jan 28 '19