title: Fastest way to signal one thread from another
body: <p>I have code that looks very similar to below. The code works, but I am curious if there are faster (lower latency) to signal one thread from another.</p>
  
  <pre><code>// condition_variable::wait (with predicate)
  #include &lt;iostream&gt;           // std::cout
  #include &lt;thread&gt;             // std::thread, std::this_thread::yield
  #include &lt;mutex&gt;              // std::mutex, std::unique_lock
  #include &lt;condition_variable&gt; // std::condition_variable
  
  std::mutex mtx;
  std::condition_variable cv;
  
  int cargo = 0;
  bool shipment_available()
  {
      return cargo!=0;
  }
  
  void consume ()
  {
      while(1)
      {
          std::unique_lock&lt;std::mutex&gt; lck(mtx);
          cv.wait(lck,shipment_available);
          // consume:
          std::cout &lt;&lt; cargo &lt;&lt; '\n';
          cargo=0;
      }
  }
  
  int main ()
  {
      std::thread consumer_thread (consume);
  
      // produce 10 items when needed:
      int i=0;
      while(1)
      {
          while (shipment_available()) std::this_thread::yield();
          std::unique_lock&lt;std::mutex&gt; lck(mtx);
          cargo = i++;
          cv.notify_one();
  
          if(i &gt; 100)
              break;
      }
  
      consumer_thread.join();
  
      return 0;
  }
  </code></pre>