Re: Synchronizing Thread Execution
Re: Synchronizing Thread Execution
- Subject: Re: Synchronizing Thread Execution
- From: Chris Suter <email@hidden>
- Date: Tue, 5 Dec 2006 14:52:37 +1100
On 05/12/2006, at 2:15 PM, Antonio Nunes wrote:
This makes the UI slightly (not prohibitively) less responsive,
when user actions trigger consecutive redraws, but it works without
blocking on my machine. Yet I have a hunch that on machines with
more than one processing unit (which unfortunately I have no access
to), racing conditions are more likely to occur, and this is not
safe. Is that correct?
It certainly isn't safe, on any processor; it's possible for there to
be a context switch after either of the while ... sleep loops, at
which point you can get one thread drawing and one thread converting.
My suggestion of using @synchronized (@"MyThreadLock") will not work
if you use it in more than one place.
However, the other suggestions should work fine:
static NSString *syncObject = @"MyConversionSyncObject";
From the helper thread:
while ((sourceObject = [e nextObject]) && !_isSaving && !_isClosing) {
…
if ([sourceObject owner]) {
@synchronized (syncObject) {
[sourceObject convertCacheToPDFDoc];
}
}
}
From drawRect:
@synchronized (syncObject) {
// Draw the required sheets
for (i = firstSheetSideIndex; i < lastSheetSideIndex; i++) {
sheetSide = [self objectInSheetSidesAtIndex:i];
[sheetSide drawWithShadow:[document showsSheetBreaks] && [ctx
isDrawingToScreen]];
}
}
I would add that I'm not sure that you want to be blocking the
drawing thread whilst conversion takes place since presumably the
conversion could be a lengthy process, but without knowing more about
what you're trying to do, I can't say for sure. A better approach
would be to use some kind of message passing (that could be a queue,
performSelectorOnMainThread, or via flag variables) between the two
threads which would minimise blocking.
- Chris
Attachment:
smime.p7s
Description: S/MIME cryptographic signature
_______________________________________________
Cocoa-dev mailing list (email@hidden)
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