Re: Synchronizing Thread Execution
Re: Synchronizing Thread Execution
- Subject: Re: Synchronizing Thread Execution
- From: Antonio Nunes <email@hidden>
- Date: Tue, 5 Dec 2006 03:15:34 +0000
On 4 Dec 2006, at 18:18, Scott Ribe wrote:
@synchronized(someUniqueObject)
Allocate a single instance of something (anything, [[NSObject
alloc] init]
would do) at startup in a global variable and synchronize on that.
Thanks Scot,
I gave it a shot and tried different variations of how and where the
variable was instantiated, but the results did not improve upon the
usage of @synchronized(self). I also tried the suggestion received
from Chris Suter to use a string like @synchronized (@"MyThreadLock").
The only solution that seems to produce the desired results is by
having ivar flags that are checked in both methods that can trigger
the conversion. It works so well that even @synchronized(self) seems
to become superfluous, at least on my single processor machine. This
way of doing it feels a bit improper though, and I'm not so sure how
it would stand up to multiprocessor/core machines:
From the helper thread:
while ((sourceObject = [e nextObject]) && !_isSaving && !_isClosing) {
…
if ([sourceObject owner]) {
while ([layoutView isDrawing]) {
[NSThread sleepUntilDate:[NSDate dateWithTimeIntervalSinceNow:
0.25]];
}
conversionInProgress = YES;
[sourceObject convertCacheToPDFDoc];
conversionInProgress = NO;
}
}
Sends the process to sleep while drawing is in progress. When it can
proceed it flags that it has triggered a conversion, and clears the
flag when done.
From drawRect:
// Wait for any conversion in progress to end
while ([document conversionInProgress]) {
[NSThread sleepUntilDate:[NSDate dateWithTimeIntervalSinceNow:0.25]];
}
// Draw the required sheets
isDrawing = YES;
for (i = firstSheetSideIndex; i < lastSheetSideIndex; i++) {
sheetSide = [self objectInSheetSidesAtIndex:i];
[sheetSide drawWithShadow:[document showsSheetBreaks] && [ctx
isDrawingToScreen]];
}
isDrawing = NO;
Flags that it is drawing some sheets, and clears the flag when done.
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?
-António
-----------------------------------------
Accepting others as they are
brings a wonderful freedom
to your own mind.
--The Peace Formula
-----------------------------------------
_______________________________________________
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