Results 1 to 10 of 10

Thread: How to use mutex in Qt multithreaded application

  1. #1
    Join Date
    Jul 2012
    Location
    India
    Posts
    33
    Thanks
    10
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Question How to use mutex in Qt multithreaded application

    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 

  2. #2
    Join Date
    Jul 2012
    Location
    India
    Posts
    33
    Thanks
    10
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: How to use mutex in Qt multithreaded application

    Please help me

  3. #3
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: How to use mutex in Qt multithreaded application

    There are a lot of problems in your code.

    A couple of things:
    1. Why are you creating a separate thread for each device?
    2. Why are you creating a new timer object every time produce() is called?
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  4. #4
    Join Date
    Jul 2012
    Location
    India
    Posts
    33
    Thanks
    10
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: How to use mutex in Qt multithreaded application

    Many thanks to your reply..,

    I am developing an application that will read some USB devices at a time in a particular time interval. So I created a thread for each device that will read the USB device itself and it will add to an array. produce() is nothing I just used it for opening the USB port after creating the object. The functionalities are working but it crashes sometime.

  5. #5
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: How to use mutex in Qt multithreaded application

    Quote Originally Posted by arunkumaraymuo1 View Post
    I am developing an application that will read some USB devices at a time in a particular time interval. So I created a thread for each device that will read the USB device itself and it will add to an array.
    There is no implication between your first sentence and your second sentence. In other words your first sentence doesn't explain your second sentence. In other words developing an application that will read some USB devices at a time in a particular time interval doesn't explain why you use threads.

    produce() is nothing I just used it for opening the USB port after creating the object.
    So why did you post it here? Post relevant code, not random code.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  6. #6
    Join Date
    Jul 2012
    Location
    India
    Posts
    33
    Thanks
    10
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: How to use mutex in Qt multithreaded application

    I am not much familiar with threads in Qt.I am not able to put my full project here so i just pasted the parts of code that uses threds in my program. What happends when I create threads like this.

    Qt Code:
    1. QList<QextPortInfo> portlist = portinfo.getPorts();
    2.  
    3. for(int iCount = 0; iCount < portlist.count(); iCount++)
    4. {
    5.  
    6.  
    7. objDeviceThread= new QThreadEx();
    8. objDevice = new SerialDevice(portlist[iCount].portName, 0);
    9. objDevice->setReadingInterval(m_iDeviceReadingInterval);
    10.  
    11. objDevice->moveToThread(objDeviceThread);
    12. objDeviceThread->start();
    13. }
    To copy to clipboard, switch view to plain text mode 

  7. #7
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: How to use mutex in Qt multithreaded application

    This code is perfectly fine.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  8. #8
    Join Date
    Jul 2012
    Location
    India
    Posts
    33
    Thanks
    10
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: How to use mutex in Qt multithreaded application

    Where is the issue?

  9. #9
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: How to use mutex in Qt multithreaded application

    We cannot possibly know why your program crashes; possibly a null/dangling pointer, possibly not. Run it in a debugger and, when it crashes, read the stack trace to find out where it crashes in your code. Then you might have a chance of discovering why it crashes by inspecting variables involved at the time. We cannot do any of these things for you.

    This would be a much easier thing to do if you were not using threads where threads are not required. Let the serial port tell you when it has data using the signals QextSerialPort gives you, buffer the data, and look at the buffers when you need to update a UI or whatever. All done in one thread.

  10. #10
    Join Date
    Jul 2012
    Location
    India
    Posts
    33
    Thanks
    10
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: How to use mutex in Qt multithreaded application

    Somtime it shows the error
    (process:2252) : GLib-ERROR **: Creating pipes for GWakeup: Too many open files

Similar Threads

  1. Named Mutex in QT
    By doggrant in forum Qt Programming
    Replies: 1
    Last Post: 26th July 2011, 13:11
  2. Mutex between two or more process
    By bred in forum Qt Programming
    Replies: 4
    Last Post: 1st November 2010, 16:43
  3. Qthread mutex
    By dognzhe in forum Qt Programming
    Replies: 5
    Last Post: 18th May 2009, 06:46
  4. Replies: 4
    Last Post: 1st December 2008, 12:13
  5. multithreaded OpenGL Qt application
    By yuriy in forum Qt Programming
    Replies: 2
    Last Post: 1st September 2006, 18:54

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.