Re: [[NSFileManager alloc] init] considered thread-safe
Re: [[NSFileManager alloc] init] considered thread-safe
- Subject: Re: [[NSFileManager alloc] init] considered thread-safe
- From: Ken Ferry <email@hidden>
- Date: Fri, 14 May 2010 10:12:20 -0700
On Wed, May 5, 2010 at 11:33 AM, Michael Ash <email@hidden> wrote:
> On Wed, May 5, 2010 at 2:29 PM, Jens Alfke <email@hidden> wrote:
> >
> > On May 5, 2010, at 8:50 AM, Michael Ash wrote:
> >
> >>> Delegates are the only reason the doc says anything other than "go
> nuts".
> >>> This is what both Jens and I were trying to say.
> >>
> >> Is this true for all OS X versions, for 10.6+, or some other
> combination?
> >
> > For all, I think, although the issue doesn’t apply prior to 10.5 because
> there wasn’t a delegate. Aside from the delegate, NSFileManager is really
> just a stateless procedural API wrapped up in an object instance, so it has
> the same thread safety as the underlying filesystem calls, i.e. “go nuts”.
>
> That's what I would have thought, but it was explicitly listed as not
> being thread safe before 10.5, and is still listed as "v10.5 and
> later" next to the entry that marks it as thread safe in the Cocoa
> thread safety summary.
>
> Just because the API is stateless doesn't mean the object itself
> doesn't have any state underneath. It *shouldn't*, but I'd tend to
> trust the API docs.
>
> However, I'm only targeting 10.5 these days, and that doc answers my
> question, so I'm in good shape! I should have looked there first.
>
There did used to be thread-safety problems.. the class was redone when the
NSError returning methods were added in 10.5.
There was one lesser thread safety fix in 10.6. +defaultManager was
previously using the simple version of a singleton method Mike discusses at
<
http://www.mikeash.com/pyblog/friday-qa-2009-10-02-care-and-feeding-of-singletons.html>.
This could potentially cause a crash if you managed to use the
defaultManager in the midst of its setup.
The workaround for 10.5 would be to call +defaultManager once from the main
thread before[1] it might happen on a background thread. This is pretty
likely to happen as a normal part of app initialization.
-Ken
[1] "before" here means something
complicated<http://en.wikipedia.org/wiki/Happened-before> if
you're trying to be exact about it, but if you do it before spawning threads
of your own, you're good.
>
> Mike
> _______________________________________________
>
> 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
>
_______________________________________________
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