Trolltech Home | Qt-interest Home | Recent Threads | All Threads | Author | Date
All threads index page 1

Qt-interest Archive, October 2006
thread pool example?


Message 1 in thread

I'm trying to make some code multi-threaded, so each CPU can work on a 
portion of the data at once. However, I'm running into some performance 
issues with thread overhead - starting up and waiting for the threads 
for each chunk of the data is actually slower than working on it on only 
one processor.

I'd like to try having a pool of sleeping threads that are already 
running, and then send them a packet of data when I want to work on the 
data, then wait for them to finish their packet, before continuing on in 
the pipeline. I think avoiding the overhead of creating/destroying the 
threads for each packet will help speed it up.

Can anyone point me to an example of a working Qt thread pool? I want to 
start up a couple of threads (one per core) when the app starts up, have 
them wait (using a wait condition of some sort) for a packet of data, 
then work on the packet when it comes in, then go back to sleep. I want 
the user of the pool to be able to wait until all packets are finished 
before continuing. This is a little more difficult than simply creating 
new threads and calling wait() on them for them to complete.

-- 
 [ signature omitted ] 

Message 2 in thread

Paul Miller wrote:
> I'm trying to make some code multi-threaded, so each CPU can work on a 
> portion of the data at once. However, I'm running into some 
> performance issues with thread overhead - starting up and waiting for 
> the threads for each chunk of the data is actually slower than working 
> on it on only one processor.
>
> I'd like to try having a pool of sleeping threads that are already 
> running, and then send them a packet of data when I want to work on 
> the data, then wait for them to finish their packet, before continuing 
> on in the pipeline. I think avoiding the overhead of 
> creating/destroying the threads for each packet will help speed it up.
>
> Can anyone point me to an example of a working Qt thread pool? I want 
> to start up a couple of threads (one per core) when the app starts up, 
> have them wait (using a wait condition of some sort) for a packet of 
> data, then work on the packet when it comes in, then go back to sleep. 
> I want the user of the pool to be able to wait until all packets are 
> finished before continuing. This is a little more difficult than 
> simply creating new threads and calling wait() on them for them to 
> complete.
>
Can Qt threads scope off of one CPU?  Its been awhile since I looked but 
I don't think they can.

Anthony

--
 [ signature omitted ] 

Message 3 in thread

Hi,

> Can Qt threads scope off of one CPU?  Its been awhile since I looked but 
> I don't think they can.

Qt threads just wrap native threads. Why do you think they can't?

--
 [ signature omitted ] 

Message 4 in thread

have a look at the mandelbrot-example.
The calculation-thread is a "forever"-loop waiting for the user to ask
for zooming in. It is not exactly what you look for, but using QThread
and QWaitCondition in that combination is a good starting point.

Greetings
Jens

Paul Miller schrieb:
> I'm trying to make some code multi-threaded, so each CPU can work on a
> portion of the data at once. However, I'm running into some performance
> issues with thread overhead - starting up and waiting for the threads
> for each chunk of the data is actually slower than working on it on only
> one processor.
> 
> I'd like to try having a pool of sleeping threads that are already
> running, and then send them a packet of data when I want to work on the
> data, then wait for them to finish their packet, before continuing on in
> the pipeline. I think avoiding the overhead of creating/destroying the
> threads for each packet will help speed it up.
> 
> Can anyone point me to an example of a working Qt thread pool? I want to
> start up a couple of threads (one per core) when the app starts up, have
> them wait (using a wait condition of some sort) for a packet of data,
> then work on the packet when it comes in, then go back to sleep. I want
> the user of the pool to be able to wait until all packets are finished
> before continuing. This is a little more difficult than simply creating
> new threads and calling wait() on them for them to complete.
> 

--
 [ signature omitted ] 

Message 5 in thread

On Sun, 2006-10-01 at 13:33 -0500, Paul Miller wrote:
> Can anyone point me to an example of a working Qt thread pool? I want to 
> start up a couple of threads (one per core) when the app starts up, have 
> them wait (using a wait condition of some sort) for a packet of data, 
> then work on the packet when it comes in, then go back to sleep. 

If you're desperate take a look at my evolvotron project (Qt3)
http://sourceforge.net/projects/evolvotron/index.htm
or
http://evolvotron.cvs.sourceforge.net/evolvotron/evolvotron/libevolvotron/
if you want to get straight to the code: 
There's a MutatableImageComputer subclassing QThread,
and a MutatableImageComputerFarm aggregating a number of them
and dishing out MutatableImageComputerTasks from a prioritized queue.
The whole thing got a bit overcomplicated when I decided worker threads
could put part-completed tasks back on the queue if something higher
priority came in.  Still, it seems to be stable and effective enough.

It's been a while since I touched the code.  I remember being frustrated
by the lack of an equivalent of win32's WaitForMultipleObjects in the
Qt3 API.  But looking at it now I'm wondering why I didn't just have
idle threads wait on QWaitCondition rather than polling for work at a
low rate.  Hmmm... maybe it's a bad example!

Tim



--
 [ signature omitted ]