Important: Please read the Qt Code of Conduct -

Qt 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.

    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:

    QThread* superThread = new QThread();
    supWorker* super = new supWorker();
    for (int i = 0; i < nrWorkers; i++){
    Worker* worker = new Worker(portRange+i);
    QThread* workerThread = new QThread();
    QThread::connect(workerThread, SIGNAL(started()), worker, SLOT(connect()));
    QThread::connect(super, SIGNAL(process(int, QString, int)), worker, SLOT(process(int,QString,int)));
    QThread::connect(worker, SIGNAL(finished(int, int, QString)), super, SLOT(handleResult(int, int, QString)));

    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.