#include <mutex>
#include <thread>
#include <chrono>
#include <iostream>
#include <condition_variable>

std::mutex              m;
std::condition_variable cv;
unsigned                counter;
constexpr unsigned      max = 10*1000*1000;
unsigned                answer = 0;

void producer()
{
  for(unsigned i=0; i<max; ++i)
  {
    std::unique_lock<std::mutex> lock(m);
    ++counter;
#if 1
    cv.notify_one();
#else
    lock.unlock();
    cv.notify_one();
#endif
  }
}

void consumer()
{
  while( answer != max )
  {
    std::unique_lock<std::mutex> lock(m);
    cv.wait(lock, [](){ return counter>0u; } );
    answer += counter;
    counter = 0;
  }
}

int main()
{
  using Clock = std::chrono::high_resolution_clock;
  const auto start = Clock::now();
  std::thread thp(producer);
  std::thread thc(consumer);
  thp.join();
  thc.join();
  const auto stop = Clock::now();
  const auto diff = stop - start;
  std::cout << "got result " << answer << " in " << std::chrono::duration_cast<std::chrono::milliseconds>(diff).count() << "[ms]" << std::endl;
}
