Re: Synchronizing Thread Execution
Re: Synchronizing Thread Execution
- Subject: Re: Synchronizing Thread Execution
- From: leenoori <email@hidden>
- Date: Mon, 4 Dec 2006 19:54:36 +0100
El 4/12/2006, a las 15:14, Antonio Nunes escribió:
On 3 Dec 2006, at 23:23, Chris Suter wrote:
I have instances of a class that can be called from two threads
at "the same time". For one method I need to ensure it is only
executed by one thread at a time, regardless of which instance of
the class is being called, and from which thread. Am I correct
that using "@synchronized(NSStringFromSelector(_cmd))" would
provide precisely this guarantee?
Yes, it would seem so.
That's what I thought too, but after some experimentation it
doesn't seem to work, or I am misunderstanding what it means. Using
the above expression it seems the objects are being modified from
somewhere else while we are in the critical region. Using
@synchronized(self) this doesn't happen. But this last expression
still doesn't provide enough protection:
Can't you just stick a file-scoped static variable somewhere and
@synchronize on that?
eg.
// single static variable shared by all instances:
static id foo = nil;
@implementation Thing
- (void)method
{
@synchronized(foo)
{
// do something
}
}
@end
_______________________________________________
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