Results 1 to 8 of 8

Thread: Seeking Suggestions for Multi-Threaded Application Design

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #7
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Seeking Suggestions for Multi-Threaded Application Design

    You'll have to decide if you want to do blocking I/O or asynchronous I/O.
    Currently you are mixing both.

    I would suggest asynchronous I/O, i.e. the code that you have in DataClient. To make that work properly don't reimplement the thread's run() method but let the base implementation run the thread's event loop.

    Something more like this

    Qt Code:
    1. class DataClient : public QObject
    2. {
    3. Q_OBJECT
    4.  
    5. signals:
    6. void dataReady();
    7.  
    8. private slots:
    9. void slotReadData()
    10. {
    11. qint64 nBytesAvailable = m_tcpSocket->bytesAvailable();
    12. if (nBytesAvailable < (int)sizeof(quint16)) // Sufficient Data NOT Arrived
    13. {
    14. return;
    15. }
    16. // Any other Data Checking Statements
    17. ...
    18.  
    19. emit dataReady();
    20. }
    21. };
    22.  
    23. class DataProcessor : public QObject
    24. {
    25. Q_OBJECT
    26.  
    27. public:
    28. DataProcessor()
    29. {
    30. // make sure m_pDataClient has the data processor as its parent, otherwise they end up in different threads.
    31.  
    32. //....
    33. connect(m_pDataClient, SIGNAL(dataReady()), this, SLOT(slotDataReady()));
    34. }
    35.  
    36. private slots:
    37. void slotDataReady(); // processing method
    38. };
    To copy to clipboard, switch view to plain text mode 

    Qt Code:
    1. DataProcessor *processor = new DataProcessor;
    2. QThread *thread = new QThread;
    3. processor->moveToThread(thread);
    To copy to clipboard, switch view to plain text mode 

    If you want the thread to stop, call thread->quit() or through a signal/slot connection (quit is a slot).

    One advantage of this approach is that you can test this without the additional threads, i.e. let the data processor do its work in the main thread, or have multiple processors on the same thread, etc.


    Cheers,
    _

  2. The following user says thank you to anda_skoa for this useful post:

    swamyonline (1st May 2014)

Similar Threads

  1. Multi-threaded rendering advice
    By jcox23 in forum Qt Programming
    Replies: 0
    Last Post: 5th November 2012, 11:25
  2. Replies: 3
    Last Post: 2nd April 2012, 09:32
  3. Multi-threaded GUI possible?
    By nurtsi in forum Qt Programming
    Replies: 12
    Last Post: 26th November 2010, 21:52
  4. Multi Threaded Client Server application
    By live_07 in forum Qt Programming
    Replies: 0
    Last Post: 27th August 2009, 16:32
  5. Design suggestions
    By vermarajeev in forum Qt Programming
    Replies: 1
    Last Post: 15th December 2006, 09:22

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
  •  
Qt is a trademark of The Qt Company.