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

Is it possible to avoid busy-waiting in a spinner? [closed]

asked 2011-04-10 13:20:59 -0600

Jim Rothrock gravatar image

I've read the Callbacks and Spinning page, and have written the following spinner:

void SteeringCtrlr::callCallbacks()
{
  static const double timeout = 0.01;
  while (node_->ok())
    cb_queue_.callAvailable(ros::WallDuration(timeout));
}

If there are no callbacks in the queue, the "while" loop will busy-wait, using a lot of CPU time. I could not find a CallbackQueue equivalent of select(). Is it possible to implement such a thing? For example, is it possible to call select() on the sockets over which topic messages and service calls are received (if sockets are used for that purpose). I can reduce the frequency with which callAvailable() is called by creating a ros::Rate object and calling sleep() on it, but I'd rather avoid busy waiting at all if possible.

edit retag flag offensive reopen merge delete

Closed for the following reason the question is answered, right answer was accepted by Jim Rothrock
close date 2015-08-24 17:58:55.154733

1 Answer

Sort by ยป oldest newest most voted
2

answered 2011-04-11 00:15:21 -0600

Lorenz gravatar image

As I understand the documentation, your loop above does not cause busy-waiting and should not at all consume a lot of CPU time. callAvailable will block and wait for an event to arrive until timeout expires. Internally, callAvailable performs a timed_wait on a condition variable that gets triggered whenever a new callback becomes available.

edit flag offensive delete link more

Comments

Thank you. I did not understand the distinction between a callback that is "ready" and one that is "available," thinking that the terms were synonymous. Also, I was confused by the phrase "wait for a callback to be available" in the documentation. I'm accustomed to GUI callback systems, in which a callback is a function, and therefore is always available. I now understand that what is meant is "wait for a callback-triggering event, such as a message from a topic, to be available." I hope this clarification helps someone.
Jim Rothrock gravatar image Jim Rothrock  ( 2011-04-13 16:19:52 -0600 )edit
Still, 0.01 seconds timeout is not very much. The CPU usage would decrease even more with a longer wait, if you can afford the additional lag on node termination.
roehling gravatar image roehling  ( 2011-09-22 06:32:40 -0600 )edit

Question Tools

Stats

Asked: 2011-04-10 13:20:59 -0600

Seen: 1,395 times

Last updated: Apr 11 '11