title: C++11 event loop with thread safe queue
body: <p>I want to create an event loop class that will run on it's own thread, support adding of tasks as <code>std::functions</code> and execute them. 
  For this, I am using the SafeQueue from here: <a href="https://stackoverflow.com/a/16075550/1069662">https://stackoverflow.com/a/16075550/1069662</a> </p>
  
  <pre><code>class EventLoop
  {
  public:
  typedef std::function&lt;void()&gt; Task;
  
  EventLoop(){ stop=false; }
  
  void add_task(Task t) { queue.enqueue(t); }
  
  void start();
  
  void stop() { stop = true; }
  
  private:
      SafeQueue&lt;Task&gt; queue;
      bool stop;
  };
  
  void EventLoop::start()
  {
      while (!stop) {
          Task t = queue.dequeue(); // Blocking call
          if (!stop) {
              t();
          }
      }    
  
      cout &lt;&lt; "Exit Loop";
  }
  </code></pre>
  
  <p>Then, you would use it like this:</p>
  
  <pre><code>EventLoop loop;
  std::thread t(&amp;EventLoop::start, &amp;loop);
  
  loop.add_task(myTask);
  // do smth else
  
  loop.stop();
  t.join();
  </code></pre>
  
  <p>My question is: how to stop gracefully the thread ?
  Here stop cannot exit the loop because of the blocking queue call.</p>