title: Thread synchronization with boost::condition_variable
body: <p>I'm doing some experiments on C++ multithreading and I have no idea how to solve one problem. Let's say we have thread pool, that process user requests using existing thread and creates new thread, when no free thread available. I've created command_queue thread-safe class, which have push and pop methods. pop waits while queue is empty and returns only when command is available or timeout occurred. Now it's time to implement thread pool. The idea is to make free threads sleep for some amount of time and kill the thread if there is nothing to do after that period of time. Here is implementation</p>
  
  <pre><code>command_queue::handler_t handler;
  while (handler = tasks.pop(timeout))
  {
      handler();
  }
  </code></pre>
  
  <p>here we exit the thread procedure if timeout occurred. That is fine, but there is problem with new thread creation. Let's say we already have 2 thread processing user requests, they are working at the moment, but we need to do some other operation asynchronously.
  We call</p>
  
  <pre><code>thread_pool::start(some_operation);
  </code></pre>
  
  <p>which should start new thread, because there is no free threads available. When thread is available it calls <code>timed_wait</code> on condition variable, so the idea is to check whether there are threads that are waiting.</p>
  
  <pre><code>if (thread_are_free_threads) // ???
     condition.notify_one();
  else
     create_thread(thread_proc);
  </code></pre>
  
  <p>but how to check it? Documentation says, that if there are no waiting threads notify_one does nothing. If I could check whether or not it did nothing that would be a solution</p>
  
  <pre><code>if (!condition.notify_one()) // nobody was notified
     create_thread(thread_proc);
  </code></pre>
  
  <p>As far as I see there is no way to check that.</p>
  
  <p>Thanks for your answers.</p>