Solved Multiple QThread with same address
-
Hi Guys,
i have some Problems with multiple Threads.
i created a thread like this example https://github.com/fabienpn/simple-qt-thread-example
... Instead of 1 THread i created two threads exactly like the first one ...
my Problem is that both threads are starting in the same adress :/
qDebug()<<"Starting worker process in Thread "<<thread()->currentThreadId();Starting worker process in Thread 0xbe0
Starting worker process in Thread 0xbe0Is it possible to start them with different threads ID because like this it doesn't seem to be working ? It only runs the second thread
Here is my Code : i don't post everything only the relevant part :)
MainWindow:
thread = new QThread(); worker = new Worker(); worker->moveToThread(thread); thread->start(); //connect(worker, SIGNAL(valueChanged()), this, SLOT(Testfunction(double))); connect(worker, SIGNAL(valueChanged(QString)), ui->IMU_Z_AXIS, SLOT(setText(QString))); connect(worker, SIGNAL(workRequested()), thread, SLOT(start())); connect(thread, SIGNAL(started()), worker, SLOT(doWork())); connect(worker, SIGNAL(finished()), thread, SLOT(quit()), Qt::DirectConnection); thread2 = new QThread(); controllerthread = new ControllerThread(); controllerthread->moveToThread(thread); thread2->start(); connect(thread2, SIGNAL(started()), controllerthread, SLOT(doWork()));
worker (Thread 1)
#include "worker.h" #include <QTimer> #include <QEventLoop> #include <QtNetwork> #include <QThread> #include <QDebug> Worker::Worker(QObject *parent) : QObject(parent) { _working =false; _abort = false; udpSocket = new QUdpSocket(this); udpSocket->bind(45455, QUdpSocket::ShareAddress); } void Worker::requestWork() { mutex.lock(); _working = true; _abort = false; qDebug()<<"Request worker start in Thread "<<thread()->currentThreadId(); mutex.unlock(); emit workRequested(); } void Worker::abort() { mutex.lock(); if (_working) { _abort = true; qDebug()<<"Request worker aborting in Thread "<<thread()->currentThreadId(); } mutex.unlock(); } void Worker::doWork() { qDebug()<<"Starting worker process in Thread "<<thread()->currentThreadId(); while(true) { qDebug()<<"Thread1"; QByteArray datagram; datagram.resize(udpSocket->pendingDatagramSize()); udpSocket->readDatagram(datagram.data(), datagram.size()); //qDebug()<<datagram.data(); double num=0; memcpy(&num, datagram, sizeof(double)); double z_Accel = datagram.toDouble(); // Checks if the process should be aborted mutex.lock(); bool abort = _abort; mutex.unlock(); if (abort) { qDebug()<<"Aborting worker process in Thread "<<thread()->currentThreadId(); break; } // This will stupidly wait 1 sec doing nothing... QEventLoop loop; QTimer::singleShot(1000, &loop, SLOT(quit())); loop.exec(); // Once we're done waiting, value is updated emit valueChanged(QString::number(z_Accel)); } // Set _working to false, meaning the process can't be aborted anymore. mutex.lock(); _working = false; mutex.unlock(); qDebug()<<"Worker process finished in Thread "<<thread()->currentThreadId(); //Once 60 sec passed, the finished signal is sent emit finished(); }
controllerthread (2nd THread)
#include "controllerthread.h" #include <QTimer> #include <QEventLoop> #include <QThread> #include <QDebug> ControllerThread::ControllerThread(QObject *parent) : QObject(parent) { _working =false; _abort = false; } void ControllerThread::requestWork() { mutex.lock(); _working = true; _abort = false; qDebug()<<"Request worker start in Thread "<<thread()->currentThreadId(); mutex.unlock(); emit workRequested(); } void ControllerThread::abort() { mutex.lock(); if (_working) { _abort = true; qDebug()<<"Request worker aborting in Thread "<<thread()->currentThreadId(); } mutex.unlock(); } void ControllerThread::doWork() { qDebug()<<"Starting worker process in Thread "<<thread()->currentThreadId(); while(true) { qDebug()<<"Thread2"; // Checks if the process should be aborted mutex.lock(); bool abort = _abort; mutex.unlock(); if (abort) { qDebug()<<"Aborting worker process in Thread "<<thread()->currentThreadId(); break; } // This will stupidly wait 1 sec doing nothing... QEventLoop loop; QTimer::singleShot(1000, &loop, SLOT(quit())); loop.exec(); // Once we're done waiting, value is updated emit valueChanged(QString::number(1)); } // Set _working to false, meaning the process can't be aborted anymore. mutex.lock(); _working = false; mutex.unlock(); qDebug()<<"Worker process finished in Thread "<<thread()->currentThreadId(); //Once 60 sec passed, the finished signal is sent emit finished(); }
-
@BadHombre
controllerthread->moveToThread(thread);
Try
thread2
instead? -
@JNBarchan Hehe, good find. That's the kind of stuff that can drive one insane :)
-
@Wieland Same thread IDs was a bit suggestive... :)
-
This post is deleted!