Re: drawing in a separate thread
Re: drawing in a separate thread
- Subject: Re: drawing in a separate thread
- From: Duncan <email@hidden>
- Date: Fri, 2 May 2008 23:40:52 -0400
On May 2, 2008, at 12:49 PM, Graham Cox <email@hidden> wrote:
Subject: Re: drawing in a separate thread
OK, I have managed to implement this after a lot of poring over the
docs. I'm not sure if it's the most efficient way to actually handle
the thread communication, but it does work (using NSMachPort). As I
hoped, there isn't a big problem with drawing the graphics as they
should be, apart from a bit of occasional blank output when the
window is resized - BUT, performance is terrible. Worse than
synchronous drawing by a long shot - I haven't measured it because
it's really obvious how much the drawing lags the user - must be an
order of magnitude slower.
Question is of course, why? The thread is calling the exact same
drawing code as the view normally does, so that's a constant. So it
must be down to the thread communication/overhead. Or perhaps just
my minimal understanding of threads, locks, ports etc. ;-)
When I need to draw, I flag that fact to the class (the class is
handling all views using a single secondary thread, but in fact I
only have tried it so far with one), and the class bundles the
update rect and view into an NSInvocation, which is placed in a
queue. An NSPortMessage is used to wake up the thread which pulls
the invocation off the queue and invokes it, which does the drawing.
Maybe the use of NSInvocation is slow? Not sure why it should be.
I think using DO IS overkill, and has too much overhead for your
application.
I've used a fairly lightweight model for worker threads. I used a
variation of ThreadSafeQueue, and created a job queue. I then created
one or more worker threads that looks in the queue for work to do.
When the thread finds work, it does the work, then blocks on a
condition lock until there is more work. It works beautifully, and
without much overhead.
In your case you'd probably only have one worker thread, and let that
thread handle all your time-consuming drawing.
See this link for info on the ThreadSafeQueue class: http://www.cocoadev.com/index.pl?ProducersAndConsumerModel
Duncan C
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden