title: Boost::asio, Shared Memory and Interprocess Communication
body: <p>I have an application that is written to use <code>boost::asio</code> exclusively as its source of input data as most of our objects are network communication based.  Due to some specific requirements, we now require the ability to use shared memory as an input method as well.  I've already written the shared memory component and it is working relatively well.</p>
  
  <p>The problem is how to handle notifications from the shared memory process to the consuming application that data is available to be read -- we need to handle the data in the existing input thread (using <code>boost::asio</code>), and we also need to not block that input thread waiting for data.</p>
  
  <p>I've implemented this by introducing an intermediate thread that waits on events to be signaled from the shared memory provider process then posts a completion handler to the input thread to handle reading in the data.</p>
  
  <p>This is working now also, but the introduction of the intermediate thread means that in a significant amount of cases we have an extra context switch before we can read the data which has a negative impact on latency, and the overhead of the additional thread is also relatively expensive.</p>
  
  <p>Here's a simplistic example of what the application is doing:</p>
  
  <pre><code>#include &lt;iostream&gt;
  using namespace std;
  
  #include &lt;boost/asio.hpp&gt;
  #include &lt;boost/thread.hpp&gt;
  #include &lt;boost/scoped_ptr.hpp&gt;
  #include &lt;boost/bind.hpp&gt;
  
  class simple_thread
  {
  public:
     simple_thread(const std::string&amp; name)
        : name_(name)
     {}
  
     void start()
     {
        thread_.reset(new boost::thread(
           boost::bind(&amp;simple_thread::run, this)));
     }
  
  private:
     virtual void do_run() = 0;
  
     void run()
     {
        cout &lt;&lt; "Started " &lt;&lt; name_ &lt;&lt; " thread as: " &lt;&lt; thread_-&gt;get_id() &lt;&lt; "\n";
        do_run();
     }
  
  
  protected:
     boost::scoped_ptr&lt;boost::thread&gt; thread_;
     std::string name_;
  };
  
  class input_thread
     : public simple_thread
  {
  public:
     input_thread() : simple_thread("Input")
     {}
  
     boost::asio::io_service&amp; svc()
     {
        return svc_;
     }
  
     void do_run()
     {
        boost::system::error_code e;
        boost::asio::io_service::work w(svc_);
        svc_.run(e);
     }
  
  private:
     boost::asio::io_service svc_;
  };
  
  struct dot
  {
     void operator()()
     {
        cout &lt;&lt; '.';
     }
  };
  
  class interrupt_thread
     : public simple_thread
  {
  public:
     interrupt_thread(input_thread&amp; input)
        : simple_thread("Interrupt")
        , input_(input)
     {}
  
     void do_run()
     {
        do
        {
           boost::this_thread::sleep(boost::posix_time::milliseconds(500));
           input_.svc().post(dot());
        }
        while(true);
     }
  
  private:
     input_thread&amp; input_;
  };
  
  int main()
  {
     input_thread inp;
     interrupt_thread intr(inp);
  
     inp.start();
     intr.start();
  
     while(true)
     {
        Sleep(1000);
     }
  }
  </code></pre>
  
  <p>Is there any way to get the data handled in the <code>input_thread</code> directly (without having to <code>post</code> it in via the <code>interrupt_thread</code>?  The assumption is that the interrupt thread is totally driven by timings from an external application (notification that data is available via a semaphore).  Also, assume that we have total control of both the consuming and providing applications, that we have additional objects that need to be handled by the <code>input_thread</code> object (so we cannot simply block and wait on the semaphore objects there).  The goal is to reduce the overhead, CPU utilization and latency of the data coming in via the shared memory providing application.</p>