title: How to Stop a QThread That Runs a Blocking Forever Loop?
body: <p>I have some rough code that I've been experimenting with:</p>
  
  <p>someserver.cpp (a GUI)</p>
  
  <pre><code>#include "server.h"
  #include "ui_server.h"
  
  Server::Server(QWidget *parent) :
      QMainWindow(parent),
      ui(new Ui::Server)
  {
      ui-&gt;setupUi(this);
  }
  
  Server::~Server()
  {
      delete ui;
  }
  
  void Server::onBtnStartClicked()
  {
      QThread worker; 
      worker.start(); // Start worker thread that goes into an infinite loop with a blocking call
  }
  
  void Server::onBtnExitClicked()
  {
      // How do I cleanly stop worker from running?
      QApplication::quit();
  }
  </code></pre>
  
  <p>worker.cpp</p>
  
  <pre><code>#include "worker.h"
  
  Worker::Worker(QObject *parent) :
      QThread(parent)
  {
  }
  
  void Worker::run()
  {
      for (;;)
      {
          // a blocking IO call here like pipe, or msgrcv
          // process data received
      }
  }
  </code></pre>
  
  <p>Since the worker thread runs in a forever loop with a blocking IO call, how will I be able to structure this so that when the Stop button is pressed in the GUI thread, the worker thread is stopped cleanly?</p>