I am creating a multithreade application to read from multiple serial devices at a time and send data to a network. My application works but I am not sure about how to perfectly use mutex in my application.
My application crashes sometimes, So I used QMutex to solve the issue.

My code is given

I created a static mutex object

Qt Code:
  1. class MyApp : public QObject
  2. {
  3. Q_OBJECT
  4.  
  5. public:
  6. explicit MyApp(QObject *parent = 0);
  7. ~MyApp();
  8.  
  9. static int s_iCount;
  10. static QList<QString> s_resultList;
  11.  
  12. static bool s_bStopThreadFlag;
  13.  
  14. static QMutex m_mutex;
  15. SerialDevice *objDevice;
  16. QThreadEx *objDeviceThread;
  17.  
  18. void removeDeviceConnction();
  19. void startDeviceReading();
  20.  
  21.  
  22.  
  23. signals:
  24. void exitThread();
  25. void sendInstructionToDevice(QString strInstructions);
  26.  
  27. private:
  28.  
  29. QString m_strResultFull;
  30.  
  31.  
  32. };
  33.  
  34.  
  35. MyApp::MyApp(QObject *parent) :
  36. QObject(parent),
  37. m_iDeviceReadingInterval(1000)
  38. {
  39.  
  40. // m_mutex.lock();
  41. s_iCount = 0;
  42. s_bStopThreadFlag = false;
  43. // m_mutex.unlock();
  44. startDeviceReading();
  45. // timerSendDataDelay->start();
  46. }
  47.  
  48.  
  49. void MyApp::startDeviceReading()
  50. {
  51.  
  52.  
  53. QList<QextPortInfo> portlist = portinfo.getPorts();
  54.  
  55. for(int iCount = 0; iCount < portlist.count(); iCount++)
  56. {
  57.  
  58.  
  59. objDeviceThread= new QThreadEx();
  60. objDevice = new SerialDevice(portlist[iCount].portName, 0);
  61. objDevice->setReadingInterval(m_iDeviceReadingInterval);
  62.  
  63. connect(this, SIGNAL(exitThread()), objDevice, SLOT(finishWork()));
  64. connect(this, SIGNAL(setReadingInterval(int)), objDevice, SLOT(setReadingInterval(int)));
  65. connect(this, SIGNAL(sendInstructionToDevice(QString)), objDevice, SLOT(sendInstructionToDevice(QString)));
  66. connect(this, SIGNAL(startReadingTimer()), objDevice, SLOT(startReadingTimer()));
  67.  
  68.  
  69. objDevice->m_strPortName = portlist[iCount].portName;
  70. objDevice->connect(objDeviceThread,
  71. SIGNAL(started()),
  72. SLOT(produce()));
  73.  
  74. objDevice->moveToThread(objDeviceThread);
  75. objDeviceThread->start();
  76. }
  77. }
To copy to clipboard, switch view to plain text mode 

Created the thred object and started the thred


And in my serial device class accesses the static variables and static mutex like this

Qt Code:
  1. void SerialDevice::produce()
  2. {
  3.  
  4.  
  5. // MyApp::m_mutex.lock();
  6. MyApp::s_iCount++;
  7. m_iThredCount = MyApp::s_iCount;
  8. MyApp::s_resultList.append("null");
  9. MyApp::s_ValueList.append("null");
  10. //MyApp::m_mutex.unlock();
  11.  
  12. PortSettings settings = {BAUD19200, DATA_8, PAR_NONE, STOP_1, FLOW_OFF, 10};
  13. m_port = new QextSerialPort(m_strPortName, settings, QextSerialPort::EventDriven);
  14.  
  15. timer = new QTimer(this);
  16. timer->setInterval(20);
  17.  
  18. m_pagevalue = new char[20];
  19.  
  20. connect(timer, SIGNAL(timeout()), SLOT(onReadyRead()));
  21. connect(m_port, SIGNAL(readyRead()), SLOT(onReadyRead()));
  22.  
  23. if (!m_port->isOpen())
  24. {
  25. m_port->setPortName(m_strPortName);
  26. m_port->open(QIODevice::ReadWrite);
  27. }
  28.  
  29.  
  30. if (m_port->isOpen() && m_port->queryMode() == QextSerialPort::Polling)
  31. timer->start();
  32. else
  33. timer->stop();
  34. }
  35.  
  36. void SerialDevice::onReadyRead()
  37. {
  38. m_strData = "";
  39. if (m_port->bytesAvailable())
  40. {
  41.  
  42. m_strData = m_port->readAll();
  43.  
  44.  
  45. // MyApp::m_mutex.lock();
  46. MyApp::s_ValueList.replace(m_iThredCount - 1, strReadings);
  47. // MyApp::m_mutex.unlock();
  48.  
  49. }
  50. }
To copy to clipboard, switch view to plain text mode