Unsolved thread not start() sometimes
-
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
-
-
Where and how do you instantiate ThreadProt?
-
Serialthread constructor call threadprot in constructor and call start().
Why this design is bad ?
-
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(QObjectpParent,Protocole* pProt) {
setParent(pParent);
... -
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?
-
thread isRunning = true isStarted is call but not call run() in this case.
-
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.
-
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.11i will update my qt in my MAC.
-
Try to call start() outside the ctor