Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Multiple QThread with same address
Forum Updated to NodeBB v4.3 + New Features

Multiple QThread with same address

Scheduled Pinned Locked Moved Solved General and Desktop
5 Posts 3 Posters 1.4k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • B Offline
    B Offline
    BadHombre
    wrote on last edited by A Former User
    #1

    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 0xbe0

    Is 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();
    }
    
    
    JonBJ 1 Reply Last reply
    0
    • B BadHombre

      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 0xbe0

      Is 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();
      }
      
      
      JonBJ Offline
      JonBJ Offline
      JonB
      wrote on last edited by
      #2

      @BadHombre
      controllerthread->moveToThread(thread);

      Try thread2 instead?

      ? 1 Reply Last reply
      2
      • JonBJ JonB

        @BadHombre
        controllerthread->moveToThread(thread);

        Try thread2 instead?

        ? Offline
        ? Offline
        A Former User
        wrote on last edited by
        #3

        @JNBarchan Hehe, good find. That's the kind of stuff that can drive one insane :)

        JonBJ 1 Reply Last reply
        2
        • ? A Former User

          @JNBarchan Hehe, good find. That's the kind of stuff that can drive one insane :)

          JonBJ Offline
          JonBJ Offline
          JonB
          wrote on last edited by
          #4

          @Wieland Same thread IDs was a bit suggestive... :)

          1 Reply Last reply
          0
          • B Offline
            B Offline
            BadHombre
            wrote on last edited by
            #5
            This post is deleted!
            1 Reply Last reply
            0

            • Login

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • Users
            • Groups
            • Search
            • Get Qt Extensions
            • Unsolved