title: Using std::conditional_variable to wait on a condition
body: <p>For simplicity, let's assume that we have only one conditional variable to match a single condition that is reflected by a boolean.</p>
  
  <p>1) Why does <code>std::condition_variable::wait(...)</code> locks the mutex again after a "notify" has been sent to un-sleep it?</p>
  
  <p>2) Seeing the behaviour in "1)", does that mean that when you do <code>std::condition_variable::notify_all</code> it only makes it so that all of the waiting threads are unblocked/woken up... but <strong>in order</strong> instead of all at once? If so, what can be done to do it all at once?</p>
  
  <p>3) If I only care about threads sleeping until a condition is met and not care a single bit for any mutex acquisition, what can I do? Is there an alternative or should current <code>std::condition_variable::wait(...)</code> approach(es) be hacked around this?</p>
  
  <p>If "hackery" is to be used, will this function work for unblocking <strong>all</strong> waiting threads on a condition and can it be called from any(per thread) threads:</p>
  
  <pre><code>//declared somehwere and modified before sending "notify"(ies)
  std::atomic&lt;bool&gt; global_shared_condition_atomic_bool;
  
  //the single(for simplicity in our case) condition variable matched with the above boolean result
  std::condition_variable global_shared_condition_variable;
  
  static void MyClass:wait()
  {
      std::mutex mutex;
      std::unique_lock&lt;std::mutex&gt; lock(mutex);
  
      while (!global_shared_condition_atomic_bool) global_shared_condition_variable.wait(lock);
  }
  </code></pre>
  
  <p>it would have been called from random "waiting" threads like so:</p>
  
  <pre><code>void random_thread_run()
  {
      while(someLoopControlValue)
      {
          //random code...
          MyClass:wait(); //wait for whatever condition the class+method is for.
          //more random code...
      }
  }
  </code></pre>
  
  <hr>
  
  <p><strong>Edit:</strong></p>
  
  <p><strong>Gate class</strong></p>
  
  <pre><code>#ifndef Gate_Header
  #define Gate_Header
  
  #include &lt;mutex&gt;
  #include &lt;condition_variable&gt;
  
  class Gate
  {
  public:
      Gate()
      {
          gate_open = false;
      }
  
      void open()
      {
          m.lock();
          gate_open = true;
          m.unlock();
  
          cv.notify_all();
      }
  
      void wait()
      {
          std::unique_lock&lt;std::mutex&gt; lock(m);
  
          while (!gate_open) cv.wait(lock);
      }
  
      void close()
      {
          m.lock();
          gate_open = false;
          m.unlock();
      }
  
  private:
      std::mutex m;
      std::condition_variable cv;
      bool gate_open;
  };
  
  #endif
  </code></pre>