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. thread not start() sometimes

thread not start() sometimes

Scheduled Pinned Locked Moved Unsolved General and Desktop
9 Posts 2 Posters 885 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.
  • K Offline
    K Offline
    kickoune103
    wrote on last edited by kickoune103
    #1

    i have application to receive value of different protocol serial ethernet .....
    i have List of protocol, i have members qThread.

    in this thread i have local object serial, ethernet, object communication is in thread.

    ex class: serialThread

    serialThread::serialThread(Protocole *Parent): ThreadProt( this, Parent )
    {
    qDebug() << "new serial thread:" << this;
    setParent(Parent); //parent = protocole
    }
    

    run is implemented here

    void serialThread::run()
    {
    
    qDebug() << "thread serial:" << QThread::currentThreadId();
    
    serial l_serial(this,m_prot);
    
     exec();
    }
    
      class ThreadProt : public QThread
    

    i have created threadProt to group code for different protocol.

    • kill thread kill parent with deleteLater (serialThread)

    • signals to thread

    • access object local in run() thread

        ThreadProt::ThreadProt(QObject*pParent,Protocole* pProt)
        {
       qDebug() << "new threadProt:" << this;
       setParent(pParent); //serialthread        Modbus thread
       m_prot = pProt;
       m_o_thread = NULL;
      connect(this, SIGNAL(finished())   ,pParent  , SLOT(deleteLater())   );
        //qthread fini on détruit le parent
        start();
      

      }

    sometimes thread not start(), not execute run() in serial thread.

    why is not the solution to inherit of qthread???

    i have restart my code with this structure.
    i prefer not use move to thread and have my object in local of thread.

    i think is better but this design not work fine if you have Intermediaire class

    1 Reply Last reply
    0
    • Christian EhrlicherC Offline
      Christian EhrlicherC Offline
      Christian Ehrlicher
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Where and how do you instantiate ThreadProt?

      Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
      Visit the Qt Academy at https://academy.qt.io/catalog

      1 Reply Last reply
      1
      • K Offline
        K Offline
        kickoune103
        wrote on last edited by
        #3

        Serialthread constructor call threadprot in constructor and call start().

        Why this design is bad ?

        1 Reply Last reply
        0
        • Christian EhrlicherC Offline
          Christian EhrlicherC Offline
          Christian Ehrlicher
          Lifetime Qt Champion
          wrote on last edited by
          #4

          And where is serialThread instantiated?
          Setting itself as parent is not such a good idea btw:

          serialThread::serialThread(Protocole Parent): ThreadProt( this, Parent )
          ThreadProt::ThreadProt(QObject
          pParent,Protocole* pProt) {
          setParent(pParent);
          ...

          Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
          Visit the Qt Academy at https://academy.qt.io/catalog

          1 Reply Last reply
          0
          • K Offline
            K Offline
            kickoune103
            wrote on last edited by
            #5

            ok, i don't know if threadProt must be use setParent.

            thread is started startProt() with call in Q_INVOKABLE dashBoardStart() function in QML.

            void App::dashBoardStart()
            {
              quint32 l_prot;
            
            
            qDebug() << "start dashboard" << LstProt.length();
            
            if (LstProt.count() ==0)
                return;
            
            for (l_prot=0;l_prot < LstProt.count();l_prot++)
            {
                Protocole* l_pt =  LstProt[l_prot].value<Protocole*>();
            
                if (m_lst_prot_ready)
                    m_lst_prot_ready->append(false);
            
            
                l_pt->startProt();
            }//Finsi
            }
            
            bool Protocole::startProt()
             {
            quint8 l_i;
            quint32 BaudRateCustom=0;
            bool l_ret = false;
            
            /* reset erreur protocol */
            CrcError = 0;
            counter   = 0;
            periodeMs = 0;
            
             qDebug() << "startProt:" << this << iLiaison << iProt <<"special:"<< isProtSpecial() << m_liaison.ConfigRs;
            
            if (isProtSpecial())
            {
                if (isModbus())
                {
            
                     if (this->m_obj_device.Modbus ==NULL)
                         this->m_obj_device.Modbus = new ModbusThread(this);
            
                }
            }
            else
            switch (iLiaison)
            {
            
                case LIAISON_RS:
            
                //allocation du thread serial
                if (m_liaison.ConfigRs == NULL)
                    m_liaison.ConfigRs = new serialThread(this);
            
                break;
            
            case LIAISON_ADC:
                if (m_liaison.adc != NULL)
                    this->m_liaison.adc->lanceThread();
                break;
            
            case LIAISON_ETH:
                    m_liaison.ConfigEth->start();
                break;
            
            case LIAISON_CAN:
            
                if (m_liaison.ConfigCAN == NULL)
                    m_liaison.ConfigCAN = new CanThread(this);
            
                break;
            }
            //if (l_ret)
            //    isActive = true;
            
            return (l_ret);
            }
            

            any idea because thread is not started if is called in inherit class?

            1 Reply Last reply
            0
            • K Offline
              K Offline
              kickoune103
              wrote on last edited by
              #6

              thread isRunning = true isStarted is call but not call run() in this case.

              1 Reply Last reply
              0
              • Christian EhrlicherC Offline
                Christian EhrlicherC Offline
                Christian Ehrlicher
                Lifetime Qt Champion
                wrote on last edited by
                #7
                if (m_liaison.ConfigRs == NULL)
                   m_liaison.ConfigRs = new serialThread(this);
                

                Is m_liaison.ConfigRs a QPointer? Otherwise I don't see how you would reuse (and restart) the thread a second time. Especially since you're calling deleteLater so m_liaison.ConfigRs will become a dangling pointer afterwards.

                Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
                Visit the Qt Academy at https://academy.qt.io/catalog

                1 Reply Last reply
                1
                • K Offline
                  K Offline
                  kickoune103
                  wrote on last edited by
                  #8

                  the problem is at start up, not a pointeur problem because constructor is started.

                  strange: in my windows pc at work, no problems.

                  on my mac, problem, i will compare version of qt.
                  my mac have qt 5.9
                  my pc have qt 5.11

                  i will update my qt in my MAC.

                  1 Reply Last reply
                  0
                  • Christian EhrlicherC Offline
                    Christian EhrlicherC Offline
                    Christian Ehrlicher
                    Lifetime Qt Champion
                    wrote on last edited by
                    #9

                    Try to call start() outside the ctor

                    Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
                    Visit the Qt Academy at https://academy.qt.io/catalog

                    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