title: How to call a function when a work item is finished in Boost.Asio?
body: <p>I would like to implement a command queue which handles incoming commands concurrently with a thread pool (so the queue grows temporarily when all threads are working). I would like to post a callback to the callers when a command worker is started and finished. My implementation is based on <a href="http://think-async.com/Asio/Recipes?skin=clean.nat,asio,pattern#A_thread_pool_for_executing_arbi" rel="nofollow">this example</a> from the Asio website. </p>
  
  <p>Is there a way to hook into these events and signal somehow? I would like to avoid the command functors knowing about the callbacks (since obviously I could call the callbacks inside the command functors).</p>
  
  <p>Pseudocode to illustrate (initialization and error handling omitted for brevity):</p>
  
  <pre><code>class CommandQueue
  {
  public:
      void handle_command(CmdId id, int param)
      {
          io_service.post(boost::bind(&amp;(dispatch_map[id]), param));
  
          // PSEUDOCODE:
          // when one of the worker threads start with this item, I want to call 
          callback_site.cmd_started(id, param);
  
          // when the command functor returns and the thread finished
          callback_site.cmd_finished(id, param);
      }
  
  private:
      boost::asio::io_service io_service;
      asio::io_service::work work;
      std::map&lt;CmdId, CommandHandler&gt; dispatch_map; // CommandHandler is a functor taking an int parameter
      CallbackSite callback_site;
  };
  </code></pre>
  
  <p>Is there a way to do this without having the command functors depend on the CallbackSite?</p>