c++ - SOLVED: Destroying thread and exiting loop -
i'm creating thread this:
main.cpp
qthread acceptorthread; acceptorobject acceptorobject; acceptorobject.setupconnections(acceptorthread, simulation); acceptorobject.movetothread(&acceptorthread);
acceptorobject.cpp
void acceptorobject::setupconnections(qthread& thread, simulation * simulation) { qobject::connect(&thread, signal(started()), this, slot(acceptnewclients())); }
acceptnewclients() method works in infinite loop. @ point if close program error:
qthread destroyed while thread still running
i looked through similar problems @ stack , 1 guy said need break loop before finishing thread in order rid of bug. suggested use flag in infinite loop , emit signal in destructor change flag , break loop. kinda worked when did this:
qobject::connect(&thread, signal(started()), this, slot(acceptnewclients())); qobject::connect(this, signal(finishthread(bool)), this, slot(acceptnewclients(bool)));
and emited finishthread(true) signal destructor directly changed flag. of course changed slot signature won't run in new thread anymore.
destructor code:
emit finishthread(true); this->thread()->quit(); if(!this->thread()->wait(3000)) { this->thread()->terminate(); this->thread()->wait(); }
how can make work?
what i've tried far:
- adding new slot change flag. result: when close program window dissapears proccess still running. think destructor destroys object before emited signal proccessed .
- making bool argument in acceptnewclients() slot default one. result: overloads funtion 1 run in different thread , second 1 tries change flag doesn't work because different functions.
solution:
connect(this, signal(finishthread()), &thread, slot(quit())); connect(this, signal(finishthread()), this, slot(deletelater()));
it pointless change slot function signature in case. in deconstructor emit finishthread() signal, nothing more.
Comments
Post a Comment