Re: Threading question
Re: Threading question
- Subject: Re: Threading question
- From: "Jonathan 'Wolf' Rentzsch" <email@hidden>
- Date: Sun, 16 May 2004 19:04:36 -0500
Ken Tozier, email@hidden, wrote:
>
What about if none of the methods actually change any values after an
>
object is created?
>
>
For example, would the "containsObject" be non thread safe? Where
>
methods below would require "synchronized"?
Yes. Immutable objects are inherently threadsafe. That's one of their
advantages.
>
@interface Foo : NSObject
>
{
>
NSArray *data;
>
}
>
>
+ (id) newFoo:(NSArray *) inArray;
>
- (id) initFoo:(NSArray *) inArray;
>
>
@end
>
>
@implementation Foo
>
>
+ (id) newFoo:(NSArray *) inArray
>
{
>
return [[[Foo alloc] init] autorelease];
>
}
>
>
- (id) initFoo:(NSArray *) inArray
>
{
>
self = [super init];
>
if (self)
>
{
>
data = [inArray retain];
>
}
>
>
return self;
>
}
>
>
- (BOOL) containsObject:(id) inObject
>
{
>
NSEnumerator *enumerator = [data objectEnumerator];
>
>
id tempObj;
>
>
while (tempObj = [enumerator nextObject])
>
{
>
if ([tempObj isEqualTo: inObject])
>
return YES;
>
}
>
>
return NO;
>
}
Be aware this code will not be threadsafe if an NSMutableArray is passed
to initFoo:. You may want to assert that you do indeed get passed an
immutable object.
| Jonathan 'Wolf' Rentzsch
http://rentzsch.com
| Red Shed Software
http://redshed.net
| "better" necessarily means "different"
_______________________________________________
cocoa-dev mailing list | email@hidden
Help/Unsubscribe/Archives:
http://www.lists.apple.com/mailman/listinfo/cocoa-dev
Do not post admin requests to the list. They will be ignored.