Results 1 to 3 of 3

Thread: Multithreading QThreads that keep a TCP connection and are reused

  1. #1
    Join Date
    Apr 2015
    Posts
    2
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11

    Default Multithreading QThreads that keep a TCP connection and are reused

    I'm really unsure of how to address this problem so i will explain it first. I need to run a number of threads that each connect to some application via TCPSocket, so far no problem. The application is quite time consuming that is why i want it to run parallel on multiple threads and one thread each to communicate with it. Once the computation is finished i want to send the results to another thread collecting the results. Therefor i wrote a Worker class:

    Qt Code:
    1. class Worker : public QObject {
    2. Q_OBJECT
    3.  
    4. public:
    5. Worker();
    6. Worker(int port);
    7. ~Worker();
    8. QTcpSocket* sock;
    9. void insert();
    10.  
    11. public slots:
    12. void connect();
    13. void process(const int &id, const QString &param, const int &arity);
    14.  
    15. signals:
    16. void ready();
    17. void finished(const int &id, const int &consistent, const QString &result);
    18. void error(QString err);
    19. };
    To copy to clipboard, switch view to plain text mode 
    Now that superThread is supposed to work off a huge file and needs to spread it around the threads, and then receive and handle the results. My approach so far is another superThread that is connected in the main() as follows:

    Qt Code:
    1. QThread* superThread = new QThread();
    2. supWorker* super = new supWorker();
    3. for (int i = 0; i < nrWorkers; i++){
    4. Worker* worker = new Worker(portRange+i);
    5. QThread* workerThread = new QThread();
    6. QThread::connect(workerThread, SIGNAL(started()), worker, SLOT(connect()));
    7. worker->moveToThread(workerThread);
    8. workerThread->start();
    9. QThread::connect(super, SIGNAL(process(int, QString, int)), worker, SLOT(process(int,QString,int)));
    10. QThread::connect(worker, SIGNAL(finished(int, int, QString)), super, SLOT(handleResult(int, int, QString)));
    11. }
    To copy to clipboard, switch view to plain text mode 
    The problem this way is obviously that i can only send the SIGNAL to all connected threads. What i want the superThread to do is send arguments to only one of the threads. I don't know how i can handle the connection so that only one of the working threads receives it?

    Any help or architectural ideas much appreciated, thanks in advance.

  2. #2
    Join Date
    Dec 2009
    Location
    New Orleans, Louisiana
    Posts
    791
    Thanks
    13
    Thanked 153 Times in 150 Posts
    Qt products
    Qt5
    Platforms
    MacOS X

    Default Re: Multithreading QThreads that keep a TCP connection and are reused

    If you want to split up a large file and processes pieces of it in separate threads and then synchronize the results, then QtConcurrent map/reduce may be much easier to implement. Have you looked at that rather than creating/managing your own threads?
    Last edited by jefftee; 1st May 2015 at 07:19.

  3. #3
    Join Date
    Apr 2015
    Posts
    2
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11

    Default Re: Multithreading QThreads that keep a TCP connection and are reused

    roughly yes, but i could not find an appropiate example that fits my needs.

Similar Threads

  1. Replies: 0
    Last Post: 2nd April 2014, 07:41
  2. Save a QScriptContext to be reused in a futur session.
    By LucStPierre in forum Qt Programming
    Replies: 0
    Last Post: 25th July 2012, 19:07
  3. Replies: 0
    Last Post: 11th November 2011, 19:18
  4. Replies: 1
    Last Post: 2nd April 2010, 06:42
  5. Qthreads
    By Sheetal in forum Qt Programming
    Replies: 5
    Last Post: 23rd March 2007, 11:12

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.