title: Calling a function in child thread in Qt?
body: <p>I have a main thread that invokes a child thread function at different times but I am not sure whether that is right way of doing it in Qt.What is wrong with the below code and looking for better alternative </p>
  
  <p>There is a main thread running infinitly when ever main thread releases the lock child does a piece of work.</p>
  
  <pre><code>#include &lt;QtCore/QCoreApplication&gt;
  #include &lt;QSemaphore&gt;
  #include &lt;QThread&gt; 
  QSemaphore sem(0);
  class Background : public QThread 
  {
  protected:
  void run() 
  { 
  for(;;)
  { 
     sem.acquire(1); 
     qDebug("Child function ran");
  } 
  } 
  };
  
  int main(int argc, char *argv[])  
  {   
  QCoreApplication a(argc, argv);   
  Background child; 
  child.start();
  qDebug("Main running"); 
  qDebug("release a lock");
  sem.release(1);
  qDebug("Do somework in main");   
  //call child
  sem.release(1);
  sem.release(1);
  return a.exec();  
  }
  </code></pre>