Boost logo

Boost Users :

Subject: [Boost-users] [asio] schedule work on all threads of a single io_service::run() pool?
From: Anthony Foiani (tkil_at_[hidden])
Date: 2013-05-17 17:32:57


Greetings --

Is there a way to ask the io_service to distribute a particular
handler to every thread that is running the service?

My use case is per-thread watchdogs, and I need the worker threads to
ping the watchdog on some regular interval (e.g., every 5 seconds).

I didn't see anything like this in the docs, and I didn't see anything
that looked promising when I looked through the source. (I'll freely
admit that the ASIO source is beyond my ability to comprehend it
without much more study, however.)

I can approximate it by having my worker threads do something like
this (pseudocode):

  while ( true )
  {
      boost::system::error_code ec;
      io_service::poll_one( ec );
      if ( ec == stopped )
          break;
      watchdog::ping();
      sleep( 500ms ); // trying to balance lag and wakeups
  }

But that has the obvious problems of lag and more wakeups than I
really need. To make this hack work, I think I need some calls that
probably don't exist; instead of that "sleep" call, I would like to be
able to do something like a timed wait on a condition variable:

      io_service::all_threads_cond.timed_wait( 5s );

That way, I would only wake up every 5 seconds (to feed the watchdog),
unless the io_service sent a "notify_one" to the condition variable
and this thread happened to be picked. Extra pings aren't much of an
issue, but it would be nice to minimize them.

Anyway. I'll continue digging, but if anyone has suggestions, I'd
love to hear them.

Thanks in advance!

Best regards,
Anthony Foiani


Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net