Re: Threads & communication performance
Re: Threads & communication performance
- Subject: Re: Threads & communication performance
- From: Shaun Wexler <email@hidden>
- Date: Mon, 8 Aug 2005 17:01:27 -0700
On Aug 8, 2005, at 3:16 PM, Laurent Michel wrote:
I have a behavior in my application that is somewhat disappointing
and puzzling. In a nutshell, the application has two threads, one
is a Cocoa thread and manages the UI, the other is compute-bound.
The compute bound thread wishes to send 'updates' to the UI thread
regularly (when events of interest occurs).
You are using a high-overhead approach which also tightly couples and
effectively single-threads the process. You need to uncouple the GUI
from the calculations, and minimize your cross-thread messaging.
There are many more efficient ways to do this, but for brevity this
should serve as an example:
#include <std_mail_disclaimer.h>
static UInt32 dataDirty __attribute__ ((aligned (4))) = 0;
- (void)calculationThread:(id)someObject
{
while ([self hasWorkToDo])
{
[self performOneIterationOfWork];
if (CompareAndSwap(0, 1, &dataDirty)) {
NSData *data = [self copyDataForDisplay];
[self performSelectorOnMainThread:@selector(updateData:)
withObject:data waitUntilDone:NO];
}
}
}
- (void)updateData:(NSData *)data
{
[matrixController setMatrixWithCalculatedData:data];
[matrix displayIfNeeded];
CompareAndSwap(1, 0, &dataDirty);
[data release];
}
If you use a custom matrix, you can add another atomic flag to -
drawRect: and nearly eliminate all subsequent cross-thread perform
messages, and Tiger will nicely limit drawing rate to max refresh for
you.
HTH~
--
Shaun Wexler
MacFOH
http://www.macfoh.com
Efficiency is intelligent laziness.
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Cocoa-dev mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden