title: Threading issues in C++
body: <p>I have asked this problem on many popular forums but no concrete response. My applciation uses serial communication to interface with external systems each having its own interface protocol. The data that is received from the systems is displayed on a GUI made in Qt 4.2.1.</p>
  
  <p>Structure of application is such that</p>
  
  <ol>
  <li><p>When app begins we have a login page
  with a choice of four modules. This
  is implemented as a maindisplay
  class. Each of the four modules is a
  separate class in itself. The concerned module here is of action class which is responsible of gathering and displaying data from various systems.</p></li>
  <li><p>User authentication gets him/her
  into the action screen. The
  constructor of the action screen
  class executes and apart from
  mundane initialisation it starts the
  individual systems threads which are
  implemented as singleton.</p></li>
  </ol>
  
  <p>Each system protocol is implemented as a singleton thread of the form:</p>
  
  <pre><code>class SensorProtocol:public QThread {    
      static SensorProtocol* s_instance;
      SensorProtocol(){}
      SensorProtocol(const SensorProtocol&amp;);
      operator=(const SensorProtocol&amp;);
  
  public:
      static SensorProtocol* getInstance();
      //miscellaneous system related data to be used for
      // data acquisition and processing
  };
  </code></pre>
  
  <p>In implementation file *.cpp:</p>
  
  <pre><code>SensorProtocol* SensorProtocol::s_instance=0;
  SensorProtocol* SensorProtocol::getInstance()
  {
     //DOUBLE CHECKED LOCKING PATTERN I have used singletons
     // without this overrated pattern also but just fyi  
     if(!s_instance)
     {
         mutex.lock();
         if(!s_instance) 
             s_instance=new SensorProtocol();
         mutex.unlock();
     } 
  }
  </code></pre>
  
  <p>Structure of run function</p>
  
  <pre><code>while(!mStop)
  {
    mutex.lock()
    while(!WaitCondition.wait(&amp;mutex,5)
    {
        if(mStop)
        return;    
    }
  
    //code to read from port when data becomes available
    // and process it and store in variables
    mutex.unlock();
  }
  </code></pre>
  
  <p>In the action screen class I have define an InputSignalHandler using sigaction and saio. This is a function pointer which is activated as soon as data arrives on any of the serial ports.</p>
  
  <p>It is a global function (we cannot change it as it is specific to Linux) which is just used to compare the file descriptors of the serial port where data has arrived and the fd's of the sensor systems,  if a match is found WaitCondition.wakeOne is invoked on that thread and it comes out the wait and reads and processes the data.  </p>
  
  <p>In the action screen class the individual threads are started as <code>SensorProtocol::getInstance()-&gt;start()</code>.  </p>
  
  <p>Each system's protocol has a frame rate at which it sends data. Based on this fact, in actions screen we set up update timers to time out at refresh rate of protocols. When these timers time out the UpdateSensorProtocol() function of operation screen is called  </p>
  
  <pre><code>connect(&amp;timer, SIGNAL(timeout), this,SLOT(UpdateSensorProtocol()));
  </code></pre>
  
  <p>This grabs an instance of sensor singleton as  </p>
  
  <pre><code>SensorProtocol* pSingleton=SensorProtocol::getInstance();
  if(pSingleton-&gt;mUpdate)
  {
      //update data on action screen GUI
     pSingleton-&gt;mUpdate=false;  //NOTE : this variable is set to
                                 // true in the singleton thread
                                 // while one frame is processed completely
  }
  </code></pre>
  
  <p>For all uses of singleton instance <code>SensorProtocol::getInstance()</code> is used. Given the above scenario, One of my protocols is hanging no matter what changes I do.</p>
  
  <p>The hang occurs in the while displaying data using UpdateSensorProtocol() If I comment <code>ShowSensorData()</code> function in the <code>UpdateSensorProtocol()</code> it works fine. But otherwise it hangs and the GUI freezes. Any suggestions!</p>
  
  <p>Also, Since the main thread grabs the running instance of singleton, is it really multithreading because we are essentially changing mUpdate in singleton itself albeit from action screen.</p>
  
  <p>I am confused in this.</p>
  
  <p>Also, Can somebody suggest an alternate design as to what I am doing now.  </p>
  
  <p>Thanks In Advance</p>