How to wait for an asio handler?

Viewed 5567

I have an object that runs around a boost::asio::io_service which has some properties. Something like that:

class Foo
{
  private:

    // Not an int in my real code, but it doesn't really matter.
    int m_bar;
    boost::asio::io_service& m_io_service;
    boost::asio::strand m_bar_strand;
};

m_bar is to be used only from a handler that is called through the strand m_bar_strand. This allows me not to lock from within those handlers.

To set the m_bar property from outside a thread that runs io_service::run() I wrote an asynchronous_setter, like so:

class Foo
{
  public:
    void async_get_bar(function<void (int)> handler)
    {
      m_bar_strand.post(bind(&Foo::do_get_bar, this, handler));
    }

    void async_set_bar(int value, function<void ()> handler)
    {
      m_bar_strand.post(bind(&Foo::do_set_bar, this, value, handler));
    }

  private:

    void do_get_bar(function<void (int)> handler)
    {
      // This is only called from within the m_bar_strand, so we are safe.

      // Run the handler to notify the caller.
      handler(m_bar);
    }

    void do_set_bar(int value, function<void ()> handler)
    {
      // This is only called from within the m_bar_strand, so we are safe.
      m_bar = value;

      // Run the handler to notify the caller.
      handler();
    }

    int m_bar;
    boost::asio::io_service& m_io_service;
    boost::asio::strand m_bar_strand;
};

This works perfectly but now I'd like to write a synchronous version of set_bar that sets the value and returns only when the set was effective. It must still guarantee that the effective set will occur within the m_bar_strand. Ideally, something reentrant.

I can imagine solutions with semaphores that would be modified from within the handler but everything I come up seems hackish and really not elegant. Is there something in Boost/Boost Asio that allows such a thing?

How would you proceed to implement this method?

3 Answers
Related