Results 1 to 8 of 8

Thread: Seeking Suggestions for Multi-Threaded Application Design

  1. #1
    Join Date
    Oct 2006
    Posts
    75
    Thanks
    10
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Seeking Suggestions for Multi-Threaded Application Design

    Hi,
    I seek your suggestions for my multi-threaded application design. The key characteristics of the application are as follow:

    1. Data reception on TCP socket from 10-15 sources (channels).
    2. Data processing for each channel - just extract data from raw bytes and assemble them into meaningful structures.
    3. Displaying data on Main Window with different sub-windows in different formats( text & graphical).
    4. Sending processed data to other systems on TCP/ UDP sockets.
    5. The display to be updated every 1 second.

    My proposal for the above:
    1. Attach QDataStream for each channel (socket) for easy access and manipulation of data.
    2. Worker thread for each data channel for processing.
    3. "After processing, storing the processed data into saperate QDataStream objects, again for easy access of data by GUI thread" or "Use of signals/ slots in threads for data communication with Main Window".

    My questions are:
    1. Which threading mechanism should I adapt for better utilization of multi-core CPU (4th generation i7 with 8GB of RAM)?
    2. Which mechanism to adapt for storing data received on TCP sockets, for easy access and processing by other threads?
    3. Is MVC mechanism suitable (in this case) for data-control as it is being accessed and utilized by other windows. Sub-Windows also include tabular displays.


    I hope I have conveyed my requirements.

    Please suggest your points, they matter a lot in my design.

    Thanks in advance.

    swamy.

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

    Default Re: Seeking Suggestions for Multi-Threaded Application Design

    There is no need to use threading for anything in the posted list. Unless you expect to be satitating the link and transfering hundreds of megabits per second you can handle all the data in the GUI thread without complicating your code with needless serializing. If after reading the data you wish to process it using threads then that's just of course fine and QtConcurrent::run() is one of the approaches you can take.
    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.


  3. The following user says thank you to wysota for this useful post:

    swamyonline (25th April 2014)

  4. #3
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,356
    Thanks
    36
    Thanked 1,533 Times in 1,484 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Seeking Suggestions for Multi-Threaded Application Design

    Quote Originally Posted by swamyonline View Post
    3. "After processing, storing the processed data into saperate QDataStream objects, again for easy access of data by GUI thread" or "Use of signals/ slots in threads for data communication with Main Window".
    Definitely the latter. Serializing and deserializing the data again just for communication within the process would be a waste of processing time.

    I am with wysota on the multithreading thing: start with a single threaded version. If the networking or processing becomes too much for the main thread, move that to a thread.

    Cheers,
    _

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

    swamyonline (25th April 2014)

  6. #4
    Join Date
    Oct 2006
    Posts
    75
    Thanks
    10
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Seeking Suggestions for Multi-Threaded Application Design

    Hi,
    Sorry for late reply as there was no access of Internet for me. Thanks "wysota" and "anda_skoa" for your suggestions.
    And any suggestions for handling socket's data: for storing (after reading from socket) and making the same data available for processing threads? I have attached QDataStream variable to QTcpSocket variable in 'DataReceiver' class and accessing the same variable in 'DataProcessorThread' class.

    It is real time data reception, processing and displaying. You can understand my worry for efficiency. .

    Regards.
    swamy.

  7. #5
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,356
    Thanks
    36
    Thanked 1,533 Times in 1,484 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Seeking Suggestions for Multi-Threaded Application Design

    When you write processing threads, do you mean you have more than one thread process the same data from one socket?

    Cheers,
    _

  8. #6
    Join Date
    Oct 2006
    Posts
    75
    Thanks
    10
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Seeking Suggestions for Multi-Threaded Application Design

    NO, only one thread. I am reading data from socket and processing the 'read data' in a 'processing thread'. Each socket is associated with a processing thread.

    // Sample Code Snippet - Not Complete

    // Data Receiving Class
    Qt Code:
    1. class DataClient: public QObject
    2. {
    3. public:
    4. DataClient();
    5. QTcpSocket m_tcpSocket;
    6. QDataStream m_dstreamData; // To be associated with socket for data access
    7. void slotReadData();
    8. };
    9. DataClient::DataClient()
    10. {
    11. // Setup Network Session and Establish Socket Connection
    12. ....
    13. m_dstreamData.setDevice(m_tcpSocket);
    14. m_dstreamData.setVersion(QDataStream::Qt_4_0);
    15. // Setup Handlers for Reading Data from Socket
    16. connect(m_tcpSocket, SIGNAL(readyRead()), this, SLOT(slotReadData()));
    17. ....
    18. }
    19. void DataClient::slotReadData()
    20. {
    21. qint64 nBytesAvailable = m_tcpSocket->bytesAvailable();
    22. if (nBytesAvailable < (int)sizeof(quint16)) // Sufficient Data NOT Arrived
    23. {
    24. return;
    25. }
    26. // Any other Data Checking Statements
    27. ...
    28. }
    To copy to clipboard, switch view to plain text mode 

    // Data Processing Thread Class

    Qt Code:
    1. class DataClient; // Forward Declaration
    2. class DataProcessor: public QThread
    3. {
    4. public:
    5. DataProcessor();
    6. DataClient *m_pDataClient;
    7. bool m_bStopped;
    8. void run();
    9. };
    10. DataProcessor::DataProcessor()
    11. {
    12. // Initialization
    13. ....
    14. m_pDataClient = new DataClient;
    15. m_bStopped = false;
    16. .....
    17. }
    18. void DataProcessor: run()
    19. {
    20. while(!m_bStopped)
    21. {
    22. // Access DataClient's Socket for Data and Process
    23. m_pDataClient->m_dstreamData >> dataItem1;
    24. m_pDataClient->m_dstreamData >> dataItem2;
    25. .....
    26. .....
    27. m_pDataClient->m_dstreamData >> dataItemn;
    28.  
    29. // Process the Data
    30. .....
    31. // Publish Processed Data to MainWindow and other Objects by Emitting Signals
    32. }
    33. }
    To copy to clipboard, switch view to plain text mode 

    So each socket (around 10 sockets for 10 data channels) is associated with a processing thread.

    Does the above scheme work fine?

    swamy.

  9. #7
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,356
    Thanks
    36
    Thanked 1,533 Times in 1,484 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,
    _

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

    swamyonline (1st May 2014)

  11. #8
    Join Date
    Oct 2006
    Posts
    75
    Thanks
    10
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Seeking Suggestions for Multi-Threaded Application Design

    Back after a while. The idea of using a 'blocking data processor' is to handle data frames of different sizes (over and under sized) read from socket. I am working on your suggestions. Thanks again.

    swamy.

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
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.