• 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
@synchronized in thread safe accessors
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

@synchronized in thread safe accessors


  • Subject: @synchronized in thread safe accessors
  • From: Nick Zitzmann <email@hidden>
  • Date: Fri, 6 Feb 2009 16:47:00 -0700

I've RTFM'd (specifically performed a full-text search in the core library for @synchronized) and STFA'd, and I'm still confused about using @synchronized in a thread-safe accessor. I can't use @synthesize because the set method must do more than just set a variable.

With NS(Recursive)Lock, this isn't difficult:

- (id)myVariable
{
	id returnValue;

	[ivarLock lock];
	returnValue = [[ivarMyVariable retain] autorelease];
	[ivarLock unlock];
	return returnValue;
}

- (void)setMyVariable:(id)newMyVariable
{
	@try
	{
		[ivarLock lock];
		if (newMyVariable != ivarMyVariable)
		{
			[ivarMyVariable release];
			ivarMyVariable = [newMyVariable retain];
			// do whatever else we need to do here
		}
	}
	@finally
	{
		[ivarLock unlock];
	}
}

OK, now what is the best way to do this using @synchronized instead of an NSLock? The getter method is obvious, but the setter isn't. My first thought was something like this:

- (void)setMyVariable:(id)newMyVariable
{
	@synchronized(ivarMyVariable)
	{
		if (newMyVariable != ivarMyVariable)
		{
			[ivarMyVariable autorelease];
			ivarMyVariable = [newMyVariable retain];
			// then go do whatever needs to be done
		}
	}
}

But would this create problems if:

(1) ivarMyVariable is nil (the documentation doesn't say what happens when @synchronized(nil) or @synchronized(<some nil variable>) is called, so I'll assume this behavior is undefined either way), or
(2) some other thread is waiting for the set method to finish in its own @synchronized() block, and after the setter is finished, the old value of ivarMyVariable gets deallocated by the autorelease pool/ garbage collector before the thread-safe block is done?


If the answer to either is yes, then should I try using some alternate mutex for an object, and if so, then what? Advice welcomed...

Nick Zitzmann
<http://www.chronosnet.com/>

_______________________________________________

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: @synchronized in thread safe accessors
      • From: Kyle Sluder <email@hidden>
  • Prev by Date: Re: Read lines from very large text file
  • Next by Date: Re: Full Screen Mode With MenuBar
  • Previous by thread: Internships available at The Omni Group
  • Next by thread: Re: @synchronized in thread safe accessors
  • Index(es):
    • Date
    • Thread