[Qt-interest] QThread and Singleton

gaurav parihar gauravparihar25 at gmail.com
Mon Jun 22 18:35:21 CEST 2009


Hi all,

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.

Structure of application is such that

1. 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.
2. 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.

*Each system protocol is implemented as a singleton thread of the form:*

*class SensorProtocol:public QThread {

static SensorProtocol* s_instance;
SensorProtocol(){}
SensorProtocol(const SensorProtocol&);
operator=(const SensorProtocol&);

public:
static SensorProtocol* getInstance();
//miscellaneous system related data to be used for data acquisition and
processing
};*

*In implementation file *.cpp:*

*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();
    }
}*

*Structure of run function*

*while(!mStop)
{
  mutex.lock()
  while(!WaitCondition.wait(&mutex,5)
  {
  if(mStop)
      return;
  }
  //code to read from port when data becomes available and process it and
store in variables
  mutex.unlock();
*}

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. 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.

In the action screen class the individual threads are started as
SensorProtocol::getInstance()->start().
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
*connect(&timer, SIGNAL(timeout), this,SLOT(UpdateSensorProtocol()));
*This grabs an instance of sensor singleton as
*SensorProtocol* pSingleton=SensorProtocol::getInstance();
*if(pSingleton->mUpdate)
*{
//update data on action screen GUI
pSingleton->mUpdate=false;  //NOTE : this variable is set to true in the
singleton thread while one frame is processed completely
}

*
*
*For all uses of singleton instance SensorProtocol::getInstance() is
used.Given the above scenario, One of my protocols is hanging no matter what
changes I do. The hang occurs in the while displaying data using
UpdateSensorProtocol() If I comment ShowSensorData() function in the
UpdateSensorProtocol() it works fine. But otherwise it hangs and the GUI
freezes. Any suggestions!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
*
*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. I am confused in this. Also, Can
somebody suggest an alternate design as to what I am doing now.
*
**Thanks In Advance**
*
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.qt-project.org/pipermail/qt-interest-old/attachments/20090622/097efe85/attachment.html 


More information about the Qt-interest-old mailing list