title: Multithreaded MVC design in C++
body: <p>I have been wroking on a design to implement a multi threaded Model-View-Controller in C++ and I stumble upon difficulties. I have some requirements I am trying to enforce:</p>
  
  <ol>
  <li><p>Make the design as simple as possible to use. Typically, when I modify the content of my model, I want the view to automatically update the display ; I don't want the programmer to have to explicitly notify the view. Ideally, an update to the model should be as simple as:</p>
  
  <p>Model model;
  [...]
  model.set_value() = 12; // Considering value is a integer here, but could be any data type</p>
  
  <p>This would trigger an update of the view</p></li>
  <li><p>Multiple threads can update the model concurrently.</p></li>
  <li><p>Possility to discriminate read and write calls to the model so read calls don't trigger a view update.</p></li>
  <li><p>As lightning fast as possible, so the use of Producer/Consumer queue,   generating a lot of small memory allocation for each message is an option I'd like to avoid.</p></li>
  </ol>
  
  <p>My solution however does not satisfy those requirements and, although it kind of work, I am not entirely satisfied ; That is why I'm turning to the community to see if a successful implementation exists out there.</p>
  
  <p>My solution first rely on the observer/observable pattern. A view is an observer and the model is an observable. Then I use macros to declare data within the model such as:</p>
  
  <pre><code>class Observer {
    void register(Callback);
    void unregister(Callback);
    void notify(Callback);
  }
  </code></pre>
  
  <p>Callback being a <code>std::function&lt;void()&gt;</code></p>
  
  <pre><code>class Type {
    // Some data Type
  }
  
  class Model : public Observer {
    // Type can be a class or just some fundamental C++ types such as int...
    DECLARE(Model, field, Type)
  }
  
  // [...]
  // Then, somewhere in the code...
  Model model;
  model.set_field().update() = some_value;
  </code></pre>
  
  <p><code>DECLARE</code> is a macro that will create get_ and set_ accessors. The set_
  accessor will return an object, let's call it Update, which will contain a
  reference to the model member. Here update is a method of this object which
  returns a reference to field which can then be updated. Upon desctruction will
  call the notify callback:</p>
  
  <pre><code>// What is the name of that design pattern ?
  &lt;template T&gt;
  class Update {
  public:
    Update(T &amp;t, Callback callback) : m_t(t), m_callback(callback)
    {}
    ~Update() { m_callback(); }
    T&amp; update() { return m_t; }
  
  private:
    T &amp;m_t;
    Callback m_callback;
  }
  </code></pre>
  
  <p>This is well and good but as I have several threads updating the model, I will need some synchronization device somewhere. A mutex in the Update object ?</p>
  
  <p>Moreover, several thread running View code will end up messing my screen (rendered with ncurses) so I also need a synchronization on the view side. I
  though of creating a thread that would run the View code and which would receive "messages" from the model upon update but I am afraid this would have a significative impact on performance.
  I don't want to implement a "digest loop" à la Angular either which would reduce the reactivity of my UI (which is a terminal app driven mainly by the keyboard).</p>
  
  <p>Anyway, did you evenr this kind of design? Do you have some pointers to
  successful implementation? Do you have some hints?</p>