[Qt-interest] QThread::sleep versus QThread::wait

Andreas Pakulat apaku at gmx.de
Tue Feb 2 22:25:01 CET 2010


On 02.02.10 15:53:34, James Matta wrote:
> I am writing a program that uses one thread to read data out of a series of
> files and push it onto a queue, and then multiple other threads to process
> the data.
> I would like to set things up so that if the event queue grows too large the
> reading thread pauses in execution so that the processing threads can catch
> up.
> My first instinct was to simply use QThread::msleep or QThread::usleep;
> however, as I was reading the documentation I noticed the QThread::wait
> function and thought that perhaps that would work, however the description
> for QThread::wait is confusing me a bit. Can anybody clarify the differences
> and perhaps give a suggestion as to which I should use?

The wait function is to be used from outside the thread to wait until
the thread terminates or the timeout is reached. So thats probably not
what you want.

However, I don't think sleep() or usleep() is the best solution either,
how do you know that after x mseconds the queue is empty again? It would
IMHO be nicer if the queue would wake up the feeding thread once enough
data has been processed. This could be done with a QWaitCondition, the
feeding-thread would wait on the condition and the queue would call
wakeOne when its empty-enough.

Andreas

-- 
You have an ambitious nature and may make a name for yourself.



More information about the Qt-interest-old mailing list