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

A question about AsyncSpinner

asked 2015-01-27 01:15:21 -0500

ZiyangLI gravatar image

Dear all,

I would like to know what would happen withros::AsyncSpinner spinner(0);

Does that mean the callback is still in the same thread as the main program?

edit retag flag offensive close merge delete

1 Answer

Sort by » oldest newest most voted
1

answered 2015-01-27 01:44:52 -0500

gvdhoorn gravatar image

updated 2015-01-27 02:45:43 -0500

According to the ros::AsyncSpinner Class Reference (from the roscpp API docs):

ros::AsyncSpinner::AsyncSpinner(uint32_t thread_count)

Simple constructor. Uses the global callback queue.

Parameters:

      thread_count: The number of threads to use. A value of 0 means to use the number of processor cores


Edit:

Then the question becomes, what would happens if I use 1?

From the source of AsyncSpinnerImpl::start() (lines 143-167):

void AsyncSpinnerImpl::start()
{
  [..]

  for (uint32_t i = 0; i < thread_count_; ++i)
  {
    threads_.create_thread(boost::bind(&AsyncSpinnerImpl::threadFunc, this));
  }
}

In this, thread_count_ is set in the ctor of AsyncSpinnerImpl to be either equal to the nr of cpus, or to the value passed in via the ctor of AsyncSpinner.

Does that mean the callback is still in the same thread as the main program? will the callback blocks the main loop?

If you specify a thread_count of 1, a single additional thread is created. So no, iiuc, the callback processing should not happen in the 'main loop'.

PS: I would've found it rather odd for a class called AsyncSpinner to return to synchronous behaviour only be changing the nr of threads it uses.

PPS: this is also partly explained in roscpp/Overview/Callbacks and Spinning - Multi-threaded Spinning on the ROS wiki.

edit flag offensive delete link more

Comments

Thank you! Then the question becomes, what would happens if I use 1? Does that mean the callback is still in the same thread as the main program? will the callback blocks the main loop?

ZiyangLI gravatar image ZiyangLI  ( 2015-01-27 02:16:06 -0500 )edit

Question Tools

1 follower

Stats

Asked: 2015-01-27 01:15:21 -0500

Seen: 891 times

Last updated: Jan 27 '15