warum sollte man mit signals/slots und ner event-loop arbeiten, wenn man lediglig nen thread beenden will ? wait() ist doch genau dafür gedacht, daß ohne event-routine zu machen.
hier mal wie sowas bei mir regelmäßig aussieht :
Code: Alles auswählen
MyThread::MyThread()
: QThread()
{
m_bLeaveThreadLoop=false;
m_bThreadLoopBlocked=false;
}
MyThread::~MyThread()
{
LeaveThreadLoop();
}
bool MyThread::StartThreadLoop(QThread::Priority eThreadPriority/*=QThread::InheritPriority*/)
{
if(isRunning()) return false; // thread already running
start(eThreadPriority); // start thread loop
return true;
}
void MyThread::LeaveThreadLoop()
{
if(isRunning()) { // if thread is active (run()-loop working) ...
m_bLeaveThreadLoop=true; // ... request to leave run()-loop
if(m_bThreadLoopBlocked) { // if thread contains blocking commands and loop is blocked yet ...
//TuWasUmDieBlockierungAufzuheben; // ... it should exist a way to unblock it, so that the loop can continue
} // (w/o such a way, thread is permitted to execute the blocking command only,
// if its termination in that position is assured w/o lose of any data
wait(500); // wait 500 ms that thread finishes its operation
if(isRunning()) { // if thread is still working after that time ...
terminate(); // ... it has to be terminated (finish it the hard way)
wait(); // and wait again until termination is done
}
}
}
void MyThread::run()
{
m_bLeaveThreadLoop=false;
while(!m_bLeaveThreadLoop) {
//DoSomethingImportant
m_bThreadLoopBlocked=true; // mark blocking commands this way
//Wait4Something(); //
m_bThreadLoopBlocked=false; //
//DoSomethingImportant
}
}
und auch gegen ein usleep ist IMHO nichts zu sage, wenn es korrekt eingebaut ist. ein generelles usleep ist natürlich quatsch, aber mit
if(NIX_ZU_TUN) usleep(100000) else { normaler thread-code }; in der thread-schleife, gibt man die CPU-zeit des thread-prozesses wieder an das BS ab, wenn sowieso nichts zu tun ist und der thread sonst nur mit 100%-last leerlaufen würde. und die sleep-zeit muß man der situation angemessen wählen, damit der lag nicht zu groß ist um zu reagieren wenn wieder etwas thread-relevantes ansteht.
Obel