PDA

View Full Version : Problem stopping a thread



tksharpless
13th September 2012, 20:40
I have an administrative thread that starts several worker threads and collates the data they produce, and I am having trouble stopping it.

Its run() loop basically looks like this:
void run()
{
runflag = true;
while(runflag)
{
take_buffers( in_fifo );
nw = start_workers(); // nw is number of active workers
exec(); // run event loop
put_buffers( out_fifo);
}
}

The event handler simply counts remaining workers:
void worker_done()
{
--nw;
if(nw <= 0) exit(); // break event loop
}

The stop() routine, to be called of course from other threads, looks something like this:
bool stop()
{
runflag = false;
in_fifo.flush();
out_fifo.flush();
return wait(500);
}
The fifo flush routines will unblock any thread waiting on a fifo operation.

Problem is, stop() is always returning false, that is, the thread did not stop, and it appears that the reason is that it is stuck in its event loop. However, the thread does stop spontaneously shortly after stop() returns. So I am wondering, does QThread::wait() somehow stop the thread's event processing, or prevent the delivery of queued signals? Or does anyone know of another reason for this odd behavior?

Qt 4.8(MinGW) on Windows Vista.

wysota
13th September 2012, 20:56
How do you actually use this code?

tksharpless
13th September 2012, 21:25
Solved. I was issuing the connect calls for the workers' signals in the same thread that would eventually call stop(), so they were in fact blocked by the wait(). Now I connect in the administrative thread, the signals are delivered to its event loop, and all works as expected.

Wysota, try to think abstractly. Of course I don't "actually use" pseudo code.

wysota
13th September 2012, 23:26
Solved. I was issuing the connect calls for the workers' signals in the same thread that would eventually call stop(), so they were in fact blocked by the wait().
That was my impression, that's why I asked how you used this code. Source code for a function is meaningless without a calling context.