Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. Mobile and Embedded
  4. why my first thread not run and 2nd thread run continuously ?
Qt 6.11 is out! See what's new in the release blog

why my first thread not run and 2nd thread run continuously ?

Scheduled Pinned Locked Moved Solved Mobile and Embedded
19 Posts 4 Posters 1.3k 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.
  • jsulmJ jsulm

    @Qt-embedded-developer said in why my first thread not run and 2nd thread run continuously ?:

    So what i need to change in above code to cleanly stop thread ?

    https://doc.qt.io/qt-5/qthread.html#isInterruptionRequested

    Q Offline
    Q Offline
    Qt embedded developer
    wrote on last edited by
    #6

    @jsulm what i need to do when blocking function occur during multi threading ?

    jsulmJ 1 Reply Last reply
    0
    • Q Qt embedded developer

      @jsulm what i need to do when blocking function occur during multi threading ?

      jsulmJ Offline
      jsulmJ Offline
      jsulm
      Lifetime Qt Champion
      wrote on last edited by
      #7

      @Qt-embedded-developer Do you mean in your thread? For how long does it block? Does that function provide a way to interrupt it?

      https://forum.qt.io/topic/113070/qt-code-of-conduct

      Q 1 Reply Last reply
      0
      • jsulmJ jsulm

        @Qt-embedded-developer Do you mean in your thread? For how long does it block? Does that function provide a way to interrupt it?

        Q Offline
        Q Offline
        Qt embedded developer
        wrote on last edited by Qt embedded developer
        #8

        @jsulm while 2 thread running .it block until i close application. you can see output i written below code.

        can you elaborate your question more i am not understand your below question :
        Does that function provide a way to interrupt it?

        jsulmJ 1 Reply Last reply
        0
        • Q Qt embedded developer

          @jsulm while 2 thread running .it block until i close application. you can see output i written below code.

          can you elaborate your question more i am not understand your below question :
          Does that function provide a way to interrupt it?

          jsulmJ Offline
          jsulmJ Offline
          jsulm
          Lifetime Qt Champion
          wrote on last edited by
          #9

          @Qt-embedded-developer Let's go one step back, you asked: "So what i need to change in above code to cleanly stop thread ?". I pointed you to https://doc.qt.io/qt-5/qthread.html#isInterruptionRequested - did you try it? If so, what is the problem now?

          https://forum.qt.io/topic/113070/qt-code-of-conduct

          Q 1 Reply Last reply
          0
          • jsulmJ jsulm

            @Qt-embedded-developer Let's go one step back, you asked: "So what i need to change in above code to cleanly stop thread ?". I pointed you to https://doc.qt.io/qt-5/qthread.html#isInterruptionRequested - did you try it? If so, what is the problem now?

            Q Offline
            Q Offline
            Qt embedded developer
            wrote on last edited by Qt embedded developer
            #10

            @jsulm i have tried it but still i got same output. i have commented blocking function call it works. but continuous running thread stop /dev/i2c-2 to get open.

            this is the main problem. and 2nd problem without comment my blocking function how to make this code get work simultaneously

            jsulmJ 1 Reply Last reply
            0
            • Q Qt embedded developer

              @jsulm i have tried it but still i got same output. i have commented blocking function call it works. but continuous running thread stop /dev/i2c-2 to get open.

              this is the main problem. and 2nd problem without comment my blocking function how to make this code get work simultaneously

              jsulmJ Offline
              jsulmJ Offline
              jsulm
              Lifetime Qt Champion
              wrote on last edited by
              #11

              @Qt-embedded-developer Please show the code.
              Also, I asked before for how long the blocking function is actually blocking?

              https://forum.qt.io/topic/113070/qt-code-of-conduct

              Q 1 Reply Last reply
              0
              • jsulmJ jsulm

                @Qt-embedded-developer Please show the code.
                Also, I asked before for how long the blocking function is actually blocking?

                Q Offline
                Q Offline
                Qt embedded developer
                wrote on last edited by Qt embedded developer
                #12

                @jsulm

                thread 1 execute for only one time then it get block. while thread 2 run continuously

                #include "dialog.h"
                #include "ui_dialog.h"
                #include <QDateTime>
                #include <QDebug>
                
                #include <QFile>
                #include <QTextStream>
                
                Dialog::Dialog(QWidget *parent)
                    : QDialog(parent)
                    , ui(new Ui::Dialog)
                {
                    ui->setupUi(this);
                
                    StartThread();
                    qDebug() << "End of Program";
                }
                
                void Dialog:: StartThread()
                {
                    HelloThread_1 thread1;
                    HelloThread_2 thread2;
                
                    thread1.start();
                    qDebug() << "hello from GUI thread " << thread1.currentThreadId();
                
                
                    thread2.start();
                    qDebug() << "hello from GUI thread " << thread2.currentThreadId();
                    thread1.wait();  // do not exit before the thread is completed!
                    thread2.wait();  // do not exit before the thread is completed!
                
                
                }
                
                
                void HelloThread_1::run()
                {
                    while(1)
                    {
                        if(QThread::currentThread()->isInterruptionRequested())
                            return;
                
                        qDebug() << "hello from worker thread AAAA";
                
                        FuelCheck ObjFuelCheck;
                        
                        ObjFuelCheck.gauge_init();
                       
                        int Percentage = -1;
                        bool bisFuelConnected = true;
                
                        ObjFuelCheck.gauge_cmd_read(ObjFuelCheck.nI2CId, 0x2c);
                qDebug() << __LINE__;
                        Percentage = ObjFuelCheck.u16Datareceived;
                
                        qDebug()<< "percentage :"<<Percentage;
                
                        QFile data("/home/user/output.txt");
                        if (data.open(QFile::WriteOnly | QFile::Append | QFile::Text)) {
                            QTextStream out(&data);
                            out << "Result: "<< QDateTime::currentDateTime().toString("dd MMM yyyy HH:mm:ss") << " "<< "percentage :"<<Percentage<<"\n";
                            // writes "Result: 3.14      2.7       "
                        }
                
                        QThread::msleep(100);
                        if((Percentage < 0) && (Percentage > 100))
                        {
                            bisFuelConnected = false;
                
                            qDebug() << "Fuel gauge found but wrong percentarge detected : " + QString::number(Percentage);
                        }
                
                        **// below  part block function **
                        FuelGaugeProfileUpdate *pFuelGaugeProfileUpdate = new FuelGaugeProfileUpdate();
                
                        if(IS_VALID_OBJ(pFuelGaugeProfileUpdate))
                        {
                           pFuelGaugeProfileUpdate->bIsRequiredToUpdateFuelGaugeProfile =true;
                          
                            if(pFuelGaugeProfileUpdate->bIsRequiredToUpdateFuelGaugeProfile)
                            {
                
                                pFuelGaugeProfileUpdate->show();
                            }
                            else
                            {
                               DELETE_OBJ(pFuelGaugeProfileUpdate);
                            }
                
                        }
                        //------------------------------------------------
                       NfcTagReaderWriter *objNFCTag_write = new NfcTagReaderWriter();
                
                        if(IS_VALID_OBJ(objNFCTag_write))
                        {
                            if(objNFCTag_write->NFCInit())
                            {
                                if(objNFCTag_write->ReadNdefTextMessage() == 0x00)
                                {
                                    qDebug()<<"WriteNdefTextMessage";
                
                                    QFile data("/home/user/output.txt");
                                    if (data.open(QFile::WriteOnly | QFile::Append | QFile::Text)) {
                                        QTextStream out(&data);
                                        out << "Result: "<< QDateTime::currentDateTime().toString("dd MMM yyyy HH:mm:ss") << " "<< "objNFCTag_write->Text.text :"<<objNFCTag_write->Text.text<<"\n";
                                        // writes "Result: 3.14      2.7       "
                                    }
                                    //objNFCTag_write->WriteNdefTextMessage();
                
                                }
                            }
                            qDebug() << __LINE__;
                            DELETE_OBJ(objNFCTag_write);
                        }
                
                qDebug() << __LINE__;
                            QThread::msleep(1000);
                
                
                    }
                }
                
                
                void HelloThread_2::run()
                {
                      while(1)
                      {
                          if(QThread::currentThread()->isInterruptionRequested())
                              return;
                           qDebug() << "hello from worker thread BBBB";
                
                           ExtAdc  ObjExtAdc;
                           AdcSettings AdcInfo;
                           uint8_t u8ReadADC_no = 2;
                           ObjExtAdc.AdcReadFile((ADC_type)(u8ReadADC_no));
                
                           QString sADC_value = QString::number(ObjExtAdc.i32AdcReadValue);
                           QFile data("/home/user/output.txt");
                           if (data.open(QFile::WriteOnly | QFile::Append | QFile::Text)) {
                               QTextStream out(&data);
                               out << "Result: "<< QDateTime::currentDateTime().toString("dd MMM yyyy HH:mm:ss") << " "<< "u16AdcValue "<<sADC_value<<"\n";
                               // writes "Result: 3.14      2.7       "
                           }
                           qDebug()<<"u16AdcValue "<<sADC_value;
                
                           /*
                           uint16_t u16AdcValue = 0;
                           bool bIsVinVolt = false;
                           float fVinVolt = 0.0;
                
                
                           ObjExtAdc.GetAvgValue(ADC_BATTERYLEVEL);
                           if(AdcInfo.bAdcReadError == true)
                           {
                            qDebug()<<"adc read error";
                           }
                           else
                           {
                               u16AdcValue = AdcInfo.u16AdcAverageValue[ADC_BATTERYLEVEL];
                
                               QFile data("/home/user/output.txt");
                               if (data.open(QFile::WriteOnly | QFile::Append | QFile::Text)) {
                                   QTextStream out(&data);
                                   out << "Result: "<< QDateTime::currentDateTime().toString("dd MMM yyyy HH:mm:ss") << " "<< "u16AdcValue "<<u16AdcValue<<"\n";
                                   // writes "Result: 3.14      2.7       "
                               }
                               qDebug()<<"u16AdcValue "<<u16AdcValue;
                
                               if(u16AdcValue < 11500)
                               {
                                   bIsVinVolt =false;
                                   //TableInsertData(sBattery(), sLow()); // + " " + QString::number(u16AdcValue));
                
                
                               }
                               else
                               {
                                   bIsVinVolt =true;
                                   //TableInsertData(sBattery() , sOk()); // + QString::number(u16AdcValue));
                
                               }
                           }
                */
                
                              QThread::msleep(1000);
                
                      }
                }
                
                Dialog::~Dialog()
                {
                    delete ui;
                }
                
                
                
                Q 1 Reply Last reply
                0
                • Q Qt embedded developer

                  @jsulm

                  thread 1 execute for only one time then it get block. while thread 2 run continuously

                  #include "dialog.h"
                  #include "ui_dialog.h"
                  #include <QDateTime>
                  #include <QDebug>
                  
                  #include <QFile>
                  #include <QTextStream>
                  
                  Dialog::Dialog(QWidget *parent)
                      : QDialog(parent)
                      , ui(new Ui::Dialog)
                  {
                      ui->setupUi(this);
                  
                      StartThread();
                      qDebug() << "End of Program";
                  }
                  
                  void Dialog:: StartThread()
                  {
                      HelloThread_1 thread1;
                      HelloThread_2 thread2;
                  
                      thread1.start();
                      qDebug() << "hello from GUI thread " << thread1.currentThreadId();
                  
                  
                      thread2.start();
                      qDebug() << "hello from GUI thread " << thread2.currentThreadId();
                      thread1.wait();  // do not exit before the thread is completed!
                      thread2.wait();  // do not exit before the thread is completed!
                  
                  
                  }
                  
                  
                  void HelloThread_1::run()
                  {
                      while(1)
                      {
                          if(QThread::currentThread()->isInterruptionRequested())
                              return;
                  
                          qDebug() << "hello from worker thread AAAA";
                  
                          FuelCheck ObjFuelCheck;
                          
                          ObjFuelCheck.gauge_init();
                         
                          int Percentage = -1;
                          bool bisFuelConnected = true;
                  
                          ObjFuelCheck.gauge_cmd_read(ObjFuelCheck.nI2CId, 0x2c);
                  qDebug() << __LINE__;
                          Percentage = ObjFuelCheck.u16Datareceived;
                  
                          qDebug()<< "percentage :"<<Percentage;
                  
                          QFile data("/home/user/output.txt");
                          if (data.open(QFile::WriteOnly | QFile::Append | QFile::Text)) {
                              QTextStream out(&data);
                              out << "Result: "<< QDateTime::currentDateTime().toString("dd MMM yyyy HH:mm:ss") << " "<< "percentage :"<<Percentage<<"\n";
                              // writes "Result: 3.14      2.7       "
                          }
                  
                          QThread::msleep(100);
                          if((Percentage < 0) && (Percentage > 100))
                          {
                              bisFuelConnected = false;
                  
                              qDebug() << "Fuel gauge found but wrong percentarge detected : " + QString::number(Percentage);
                          }
                  
                          **// below  part block function **
                          FuelGaugeProfileUpdate *pFuelGaugeProfileUpdate = new FuelGaugeProfileUpdate();
                  
                          if(IS_VALID_OBJ(pFuelGaugeProfileUpdate))
                          {
                             pFuelGaugeProfileUpdate->bIsRequiredToUpdateFuelGaugeProfile =true;
                            
                              if(pFuelGaugeProfileUpdate->bIsRequiredToUpdateFuelGaugeProfile)
                              {
                  
                                  pFuelGaugeProfileUpdate->show();
                              }
                              else
                              {
                                 DELETE_OBJ(pFuelGaugeProfileUpdate);
                              }
                  
                          }
                          //------------------------------------------------
                         NfcTagReaderWriter *objNFCTag_write = new NfcTagReaderWriter();
                  
                          if(IS_VALID_OBJ(objNFCTag_write))
                          {
                              if(objNFCTag_write->NFCInit())
                              {
                                  if(objNFCTag_write->ReadNdefTextMessage() == 0x00)
                                  {
                                      qDebug()<<"WriteNdefTextMessage";
                  
                                      QFile data("/home/user/output.txt");
                                      if (data.open(QFile::WriteOnly | QFile::Append | QFile::Text)) {
                                          QTextStream out(&data);
                                          out << "Result: "<< QDateTime::currentDateTime().toString("dd MMM yyyy HH:mm:ss") << " "<< "objNFCTag_write->Text.text :"<<objNFCTag_write->Text.text<<"\n";
                                          // writes "Result: 3.14      2.7       "
                                      }
                                      //objNFCTag_write->WriteNdefTextMessage();
                  
                                  }
                              }
                              qDebug() << __LINE__;
                              DELETE_OBJ(objNFCTag_write);
                          }
                  
                  qDebug() << __LINE__;
                              QThread::msleep(1000);
                  
                  
                      }
                  }
                  
                  
                  void HelloThread_2::run()
                  {
                        while(1)
                        {
                            if(QThread::currentThread()->isInterruptionRequested())
                                return;
                             qDebug() << "hello from worker thread BBBB";
                  
                             ExtAdc  ObjExtAdc;
                             AdcSettings AdcInfo;
                             uint8_t u8ReadADC_no = 2;
                             ObjExtAdc.AdcReadFile((ADC_type)(u8ReadADC_no));
                  
                             QString sADC_value = QString::number(ObjExtAdc.i32AdcReadValue);
                             QFile data("/home/user/output.txt");
                             if (data.open(QFile::WriteOnly | QFile::Append | QFile::Text)) {
                                 QTextStream out(&data);
                                 out << "Result: "<< QDateTime::currentDateTime().toString("dd MMM yyyy HH:mm:ss") << " "<< "u16AdcValue "<<sADC_value<<"\n";
                                 // writes "Result: 3.14      2.7       "
                             }
                             qDebug()<<"u16AdcValue "<<sADC_value;
                  
                             /*
                             uint16_t u16AdcValue = 0;
                             bool bIsVinVolt = false;
                             float fVinVolt = 0.0;
                  
                  
                             ObjExtAdc.GetAvgValue(ADC_BATTERYLEVEL);
                             if(AdcInfo.bAdcReadError == true)
                             {
                              qDebug()<<"adc read error";
                             }
                             else
                             {
                                 u16AdcValue = AdcInfo.u16AdcAverageValue[ADC_BATTERYLEVEL];
                  
                                 QFile data("/home/user/output.txt");
                                 if (data.open(QFile::WriteOnly | QFile::Append | QFile::Text)) {
                                     QTextStream out(&data);
                                     out << "Result: "<< QDateTime::currentDateTime().toString("dd MMM yyyy HH:mm:ss") << " "<< "u16AdcValue "<<u16AdcValue<<"\n";
                                     // writes "Result: 3.14      2.7       "
                                 }
                                 qDebug()<<"u16AdcValue "<<u16AdcValue;
                  
                                 if(u16AdcValue < 11500)
                                 {
                                     bIsVinVolt =false;
                                     //TableInsertData(sBattery(), sLow()); // + " " + QString::number(u16AdcValue));
                  
                  
                                 }
                                 else
                                 {
                                     bIsVinVolt =true;
                                     //TableInsertData(sBattery() , sOk()); // + QString::number(u16AdcValue));
                  
                                 }
                             }
                  */
                  
                                QThread::msleep(1000);
                  
                        }
                  }
                  
                  Dialog::~Dialog()
                  {
                      delete ui;
                  }
                  
                  
                  
                  Q Offline
                  Q Offline
                  Qt embedded developer
                  wrote on last edited by
                  #13

                  @Qt-embedded-developer Just for your information all devices connected to same i2c bus

                  Q 1 Reply Last reply
                  0
                  • Q Qt embedded developer

                    @Qt-embedded-developer Just for your information all devices connected to same i2c bus

                    Q Offline
                    Q Offline
                    Qt embedded developer
                    wrote on last edited by Qt embedded developer
                    #14

                    @Qt-embedded-developer i have found that there i am getting error due to
                    " Errno 24 : Too many open files" on /dev/i2c-2"

                    JonBJ 1 Reply Last reply
                    0
                    • Q Qt embedded developer

                      @Qt-embedded-developer i have found that there i am getting error due to
                      " Errno 24 : Too many open files" on /dev/i2c-2"

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

                      @Qt-embedded-developer
                      Linux errno 24 (EMFILE) is too many open files for your process (or maybe your user, but not I think for the system). I think you can alter this via ulimit, or others. But obviously you need to understand where you are keeping open file handles from.

                      Q 1 Reply Last reply
                      0
                      • JonBJ JonB

                        @Qt-embedded-developer
                        Linux errno 24 (EMFILE) is too many open files for your process (or maybe your user, but not I think for the system). I think you can alter this via ulimit, or others. But obviously you need to understand where you are keeping open file handles from.

                        Q Offline
                        Q Offline
                        Qt embedded developer
                        wrote on last edited by
                        #16

                        @JonB means i need to open file out side thread.

                        not inside thread.

                        am i correct ?

                        jsulmJ 1 Reply Last reply
                        0
                        • Q Qt embedded developer

                          @JonB means i need to open file out side thread.

                          not inside thread.

                          am i correct ?

                          jsulmJ Offline
                          jsulmJ Offline
                          jsulm
                          Lifetime Qt Champion
                          wrote on last edited by
                          #17

                          @Qt-embedded-developer said in why my first thread not run and 2nd thread run continuously ?:

                          am i correct ?

                          No, you are wrong. You just have to make sure you don't open too many files at once.

                          I ask now for the third (and last) time: for how long does your blocking function block?

                          https://forum.qt.io/topic/113070/qt-code-of-conduct

                          Q 1 Reply Last reply
                          2
                          • jsulmJ jsulm

                            @Qt-embedded-developer said in why my first thread not run and 2nd thread run continuously ?:

                            am i correct ?

                            No, you are wrong. You just have to make sure you don't open too many files at once.

                            I ask now for the third (and last) time: for how long does your blocking function block?

                            Q Offline
                            Q Offline
                            Qt embedded developer
                            wrote on last edited by Qt embedded developer
                            #18

                            @jsulm 30 to 50 second and infinite when some error happen while i2c write

                            jsulmJ 1 Reply Last reply
                            0
                            • Q Qt embedded developer

                              @jsulm 30 to 50 second and infinite when some error happen while i2c write

                              jsulmJ Offline
                              jsulmJ Offline
                              jsulm
                              Lifetime Qt Champion
                              wrote on last edited by
                              #19

                              @Qt-embedded-developer Then it should work as long as no errors happen with isInterruptionRequested if you use it correctly. But from your code I do not see where you actually request the thread to stop (you just wait for the threads)? If you do not ask it to stop why should it stop?!
                              If you read https://doc.qt.io/qt-5/qthread.html#isInterruptionRequested you will find link to https://doc.qt.io/qt-5/qthread.html#requestInterruption which you can use to ask the thread to stop.

                              https://forum.qt.io/topic/113070/qt-code-of-conduct

                              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