title: Multi-threading in a sender-receiver model in C++
body: <p>I am trying to build a sender-receiver model in C++. I want to send data from sender to receiver in the feed-forward way. Shortly, the desired architecture could be expressed like so (elementary units are called <em>nodes</em>):</p>
  
  <blockquote>
    <p>Each node can receive (send) data from (to) another node. Each node has as many as desired senders (receivers). Each node has a number called <em>impulse</em> which is data supposed to be sent. Let's call one data propagation through a whole network a <em>cycle</em>. Then in one cycle each node that belongs to the network must send impulse once and only once to all its receivers.</p>
  </blockquote>
  
  <p>A scratch of the implementation of the above idea is the following.</p>
  
  <pre><code>class Node
  {
    private :
      double in_signal;//received data
      double out_signal;//data to send
      bool is_opened;//status of channel
  
      void update_in( double package );//receive new package
  
    protected :
      std::vector&lt;Node*&gt; receivers;
  
    public :
      Node( double out_signal )
      : in_signal(0.),
        out_signal(out_signal),
        is_opened(false)
      {}
      ~Node() {}
  
      void add_receiver( Node* receiver );
      void emit();//send out_signal or impulse
  };
  
  void Node::update_in( double package )
  {
    //the problem is how to control the status of the channel:
    //it must be closed after getting all data
    if ( not is_opened )
    {
      in_signal = 0.;//reset
      is_opened = true;
    }
    in_signal += package;
  }
  
  void Node::add_receiver( Node* receiver )
  {
     receivers.push_back( receiver );
  }
  
  void Node::emit()
  {
     for ( auto&amp; receiver : receivers )
     {
        receiver-&gt;update_in( out_signal );
     }
  } 
  </code></pre>
  
  <p>The problem I cannot solve is <em>multi-threading behind this architecture</em>:</p>
  
  <blockquote>
    <p>The node's <code>in_signal</code> can be updated by several senders, but senders work independently (required) therefore I am afraid of arising concurrency.</p>
  </blockquote>
  
  <hr>
  
  <p>So I am asking about </p>
  
  <ol>
  <li>How to solve this multi-thereading problem?</li>
  <li>How to decide if all data has been received?</li>
  </ol>
  
  <p>I will be thankful for some ideas, patterns, conceptions etc. </p>