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. Real Time Operation - QThread?
QtWS25 Last Chance

Real Time Operation - QThread?

Scheduled Pinned Locked Moved General and Desktop
qt5
17 Posts 4 Posters 5.7k Views
  • 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.
  • A Offline
    A Offline
    alex_malyu
    wrote on last edited by
    #6

    It is tough to say anything why not all the code is present in QThread
    case. It is very sensitive to usage.

    I would recommend reading sorses on the web, like :
    http://blog.debao.me/2013/08/how-to-use-qthread-in-the-right-way-part-1/

    Lays147L 1 Reply Last reply
    0
    • A alex_malyu

      It is tough to say anything why not all the code is present in QThread
      case. It is very sensitive to usage.

      I would recommend reading sorses on the web, like :
      http://blog.debao.me/2013/08/how-to-use-qthread-in-the-right-way-part-1/

      Lays147L Offline
      Lays147L Offline
      Lays147
      wrote on last edited by Lays147
      #7

      @alex_malyu code of my thread

      #include "temperaturesthread.h"
      
      TemperaturesThread::TemperaturesThread(Repetier *printer)
      {
          this->printer = printer;
          this->stopLoop = false;
      }
      
      void TemperaturesThread::run()
      {
          while(true)
          {   mutexIno.lock();
              QList<double> extrudersTemp;
                  for(int i = 0; i < printer->getNoOfExtruders();i++)
                  {
                      extrudersTemp.append(printer->getExtruderTemp(i));
                  }
              mutexIno.unlock();
              usleep(100000);
              emit updateTemp(extrudersTemp,printer->getBedTemp());
              if(stopLoop==true)
                      break;
          }
      
      }
      
      void TemperaturesThread::pausar(bool b)
      {
          if(b==true)
           this->mutexIno.lock();
          else
           this->mutexIno.unlock();
      }
      
      void TemperaturesThread::setLoop(bool b)
      {   QMutexLocker locker(&mutexIno);
          this->stopLoop = b;
      }
      bool TemperaturesThread::getLoop()
      {
      }
      

      Lays Rodrigues
      Newby on Qt - Learning always!
      Using QT 5.7
      ArchLinux

      1 Reply Last reply
      0
      • Lays147L Offline
        Lays147L Offline
        Lays147
        wrote on last edited by
        #8

        When i disconnect the arduino, works right, but when i click on button, the program crash. Without call a exception or something else. The thread looks work right.

        Lays Rodrigues
        Newby on Qt - Learning always!
        Using QT 5.7
        ArchLinux

        1 Reply Last reply
        0
        • A Offline
          A Offline
          alex_malyu
          wrote on last edited by
          #9

          How is yours mutex is created? Is it a recursive mutex?

          Lays147L 1 Reply Last reply
          0
          • A alex_malyu

            How is yours mutex is created? Is it a recursive mutex?

            Lays147L Offline
            Lays147L Offline
            Lays147
            wrote on last edited by Lays147
            #10

            @alex_malyu
            on my class brprint3d.h
            private: QMutex mutexIno;
            I need to call a constructor with mutexIno(1) ?

            Lays Rodrigues
            Newby on Qt - Learning always!
            Using QT 5.7
            ArchLinux

            A 1 Reply Last reply
            0
            • Lays147L Lays147

              @alex_malyu
              on my class brprint3d.h
              private: QMutex mutexIno;
              I need to call a constructor with mutexIno(1) ?

              A Offline
              A Offline
              alex_malyu
              wrote on last edited by
              #11

              @Lays147

              From 4.8 documentation:

              Calling this function multiple times on the same mutex from the same thread is allowed if this mutex is a recursive mutex. If this mutex is a non-recursive mutex, this function will dead-lock when the mutex is locked recursively.

              This means

              QMutexLocker locker(&mutexIno);
              mutexIno.lock();

              was already dead lock

              Lays147L 1 Reply Last reply
              0
              • A alex_malyu

                @Lays147

                From 4.8 documentation:

                Calling this function multiple times on the same mutex from the same thread is allowed if this mutex is a recursive mutex. If this mutex is a non-recursive mutex, this function will dead-lock when the mutex is locked recursively.

                This means

                QMutexLocker locker(&mutexIno);
                mutexIno.lock();

                was already dead lock

                Lays147L Offline
                Lays147L Offline
                Lays147
                wrote on last edited by
                #12

                @alex_malyu but i only calls QMutexLock OR QMutex, not both. I'm just trying to discover which works better.
                Because with one or another the program crashs.

                Lays Rodrigues
                Newby on Qt - Learning always!
                Using QT 5.7
                ArchLinux

                A 1 Reply Last reply
                0
                • Lays147L Lays147

                  @alex_malyu but i only calls QMutexLock OR QMutex, not both. I'm just trying to discover which works better.
                  Because with one or another the program crashs.

                  A Offline
                  A Offline
                  alex_malyu
                  wrote on last edited by alex_malyu
                  #13

                  @Lays147

                  did you find which statement crashes code?
                  This mutex is created somewhere, are you sure it is not locked from the same tread in the other place?

                  1 Reply Last reply
                  0
                  • A Offline
                    A Offline
                    alex_malyu
                    wrote on last edited by alex_malyu
                    #14

                    and really it should not make much difference beside maybe a bit slower construction in the case of QMutexLocker.

                    1 Reply Last reply
                    0
                    • Lays147L Offline
                      Lays147L Offline
                      Lays147
                      wrote on last edited by
                      #15

                      I initiaze my QMutex with recursion on my constructor here:
                      BrPrint3D::BrPrint3D(QWidget *parent) : QMainWindow(parent),mutexIno(QMutex::Recursive),
                      But dont solve my problem.
                      The program still crash when i click on the button.
                      I'm trying to do this way for test:

                      void BrPrint3D::on_bt_table_clicked(bool checked)
                      {   //QMutexLocker locker(&mutexIno);
                          if(mutexIno.tryLock(200))
                          {
                          qDebug() <<"Lock Done";
                          if(checked==true)
                          {
                              ui->bt_table->setStyleSheet("background-color:red;");
                              float temp=ui->tb_Table_Temperature->text().toFloat();
                              printer_object->setBedTemp(temp)==true ? qDebug() <<"OK": qDebug()<<"~OK";
                      
                          }
                          else
                          {
                              ui->bt_table->setStyleSheet("");
                              printer_object->setBedTemp(0);
                      
                          }
                          mutexIno.unlock();
                        }
                          else
                              qDebug()<<"Timeout";
                      }
                      

                      the qDebug()<<"Lock Done" appear, but the unlock dont.

                      This is the first time that i use QThread and QMutex, please help me.

                      Lays Rodrigues
                      Newby on Qt - Learning always!
                      Using QT 5.7
                      ArchLinux

                      1 Reply Last reply
                      0
                      • A Offline
                        A Offline
                        alex_malyu
                        wrote on last edited by alex_malyu
                        #16

                        @Lays147 said:

                        You need to find the exact line it crashes at.

                        Put debug statements at every line like below.
                        The last statement might tell you where it crashes.

                        First candidate to check is probably printer_object->setBedTemp()
                        another candidate to check will be places you lock the same mutex,
                        or access the same objects and forgot to use mutex
                        You may keep putting printing statements to find it out.

                        Finally try to produce small compilable example which reproduces the problem.
                        Someone's code always requires time to understand,
                        but incomplete code often brings nothing but headache, especially when multi-threading is involved.

                        void BrPrint3D::on_bt_table_clicked(bool checked)
                        { //QMutexLocker locker(&mutexIno);
                        if(mutexIno.tryLock(200))
                        {
                        qDebug() <<"Lock Done";
                        if(checked==true)
                        {
                        qDebug() <<" after lock 1";
                        ui->bt_table->setStyleSheet("background-color:red;");
                        qDebug() <<" after lock 2";
                        float temp=ui->tb_Table_Temperature->text().toFloat();
                        qDebug() <<" after lock 3";
                        printer_object->setBedTemp(temp)==true ? qDebug() <<"OK": qDebug()<<"~OK";
                        qDebug() <<" after lock 4";

                        }
                        else
                        {
                        qDebug() <<" after lock 5";
                            ui->bt_table->setStyleSheet("");
                        qDebug() <<" after lock 6";
                            printer_object->setBedTemp(0);
                        
                        }
                        qDebug() <<" before unlock";
                        mutexIno.unlock();
                        qDebug() <<" after unlock";>   }
                        else
                            qDebug()<<"Timeout";
                        

                        }

                        1 Reply Last reply
                        0
                        • Lays147L Offline
                          Lays147L Offline
                          Lays147
                          wrote on last edited by
                          #17

                          Hi everyone!
                          I found it the error, was in a string test.
                          Now I am testing all Arduino commands without the temperature thread.
                          But thanks to help from everyone!

                          Lays Rodrigues
                          Newby on Qt - Learning always!
                          Using QT 5.7
                          ArchLinux

                          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