• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: Synchronizing Thread Execution
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

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

  • Follow-Ups:
    • [Solved] Re: Synchronizing Thread Execution
      • From: Antonio Nunes <email@hidden>
References: 
 >Re: Synchronizing Thread Execution (From: Scott Ribe <email@hidden>)
 >Re: Synchronizing Thread Execution (From: Antonio Nunes <email@hidden>)

  • Prev by Date: Re: Responder chain vs find panel
  • Next by Date: Re: Synchronizing Thread Execution
  • Previous by thread: Re: Synchronizing Thread Execution
  • Next by thread: [Solved] Re: Synchronizing Thread Execution
  • Index(es):
    • Date
    • Thread