Skip navigation.
 
mlRe: NSFileManager and thread safety
FROM : Bill Bumgarner
DATE : Mon Jun 23 19:38:21 2008

On Jun 23, 2008, at 9:55 AM, Christiaan Hofman wrote:
> Perhaps, but I *should* be able to get a definite answer by reading 
> the docs. In the absence of explicit documentation, I should expect 
> that the *normal* behavior should be supported. The normal behavior 
> for a thread-unsafe class is that different instances (AOT the same 
> instance) *can* be accessed from different threads, as it is for 
> e.g. NSMutableDictionary (for which there also does not explicit 
> statement that this would be true). As the NSFileManager docs don't 
> explicitly state otherwise, I would therefore expect to be able to 
> use different instances from different threads.
>
> Note that if your arguments were valid, it should just as well apply 
> to e.g. NSMutableDictionary. I am pretty sure though that 
> NSMutableDictionary can be used on secondary threads.


The Cocoa Threading documentation says:
...

Mutable objects are generally not thread-safe. To use mutable objects 
in a threaded application, the application must synchronize access to 
them using locks. (For more information, see “Atomic Operations”). In 
general, the collection classes (for example, NSMutableArray, 
NSMutableDictionary) are not thread-safe when mutations are concerned. 
That is, if one or more threads are changing the same array, problems 
can occur. You must lock around spots where reads and writes occur to 
assure thread safety.

...

Which isn't a terribly strong statement regarding use of multiple 
instances in different threads, but there you have it.  It should 
likely be clarified.

As per NSFileManager, the release notes say:

In versions of Mac OS X prior to Leopard, the only supported 
NSFileManager instance was the instance returned from +[NSFileManager 
defaultManager] (a singleton instance). Calling [[NSFileManager alloc] 
init] would return a new object instance of NSFileManager, but this 
instance might behave strangely in the face of multiple threads.

In Leopard, calling +[NSFileManager defaultManager] still gets you the 
same singleton instance no matter when or in what thread you call it, 
but calling [[NSFileManager alloc] init] is now explicitly supported 
and returns a new instance.

The intention was to say "individual instances of NSFileManager can be 
used from separate threads simultaneously.  This does not mean that 
an instance, itself, is thread safe."

So, yes, it is supported and the documentation will be augmented in 
the near future.  The easiest way to achieve successfully threaded 
use of NSFileManager instances is to isolate each instance to a thread.

Definitely a documentation issue and an appropriate bug has been filed.

b.bum

Related mailsAuthorDate
mlNSFileManager and thread safety Christiaan Hofman Jun 23, 16:48
mlRe: NSFileManager and thread safety Andrew Merenbach Jun 23, 17:36
mlRe: NSFileManager and thread safety Adam R. Maxwell Jun 23, 17:45
mlRe: NSFileManager and thread safety Bill Bumgarner Jun 23, 17:58
mlRe: NSFileManager and thread safety Clark Cox Jun 23, 18:27
mlRe: NSFileManager and thread safety Christiaan Hofman Jun 23, 18:55
mlRe: NSFileManager and thread safety Bill Bumgarner Jun 23, 19:38