• 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
Thread safety with background fetching
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Thread safety with background fetching


  • Subject: Thread safety with background fetching
  • From: Ben <email@hidden>
  • Date: Thu, 31 Jul 2008 17:59:20 +0100

Hi List,

Having read the Apple documentation and beginner guides on threading, I am still a little confused about protecting my variables from access by multiple threads.

A sample of my code is as follows:

@interface Class1 : NSObject
{
	XMLFetcherParser *fetcherParser;
}
- (void)doBackgroundFetch;
- (void)receiveItem:(Item *)newItem;
@end

@implementation Class1

- (void)doBackgroundFetch
{
	NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];

	fetcherParser = [[XMLFetcherParser alloc] init];
	[fetcherParser doYourThing];

	[fetcherParser release];
	[pool release];
}

- (void)receiveItem:(Item *)newItem
{
	// Add newItem to an array
}

@end

For usage, the summary is that I do:

[NSThread detachNewThreadSelector:@selector(doBackgroundFetch) toTarget:self withObject:nil];

and then -[XMLFetcherParser doYourThing] repeatedly sends newly created objects to receiveItem: using the performSelectorOnMainThread method. Once the spawned thread has done this, it never uses the sent object again.


My question is how should I protect this code from thread related problems? Do I just place @synchronized(){} blocks around the code in receiveItem: and it's counterpart in the spawned thread? Or is that just plain wrong? Or (likely) am I barking up totally the wrong tree?



Regards,

Ben

_______________________________________________

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


  • Follow-Ups:
    • Re: Thread safety with background fetching
      • From: Phil <email@hidden>
    • Re: Thread safety with background fetching
      • From: Jens Alfke <email@hidden>
    • Re: Thread safety with background fetching
      • From: Joseph Kelly <email@hidden>
  • Prev by Date: Re: Problem binding NSTextView's attributedString
  • Next by Date: Re: Thread safety with background fetching
  • Previous by thread: Should you unbind an ArrayController for performance reasons?
  • Next by thread: Re: Thread safety with background fetching
  • Index(es):
    • Date
    • Thread