Re: Thread crashing problem
Re: Thread crashing problem
- Subject: Re: Thread crashing problem
- From: Ken Tozier <email@hidden>
- Date: Mon, 22 Dec 2008 10:02:33 -0500
Found a partial solution but it's ugly. If I copy the array before
enumerating it, no more crashes, but I'd prefer to not add that
overhead...
On Dec 22, 2008, at 9:24 AM, Ken Tozier wrote:
Hi
I wrote a little app to do deep folder watching on Windows servers
from a Mac and am getting crashes when directories are added or
removed from the watch directory. The directory scanning takes place
in a thread and I tried to isolate the array from outside changes by
locking it, but its still getting mutated out from underneath me.
Here's the method that executes in a thread
- (void) updateDirectoriesInThread:(id) inData
{
NSLock *lock = [[NSLock alloc] init];
busy = YES;
if ([lock tryLock])
{
// allocate a new autorelease pool
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
NSEnumerator *enumerator = [directories objectEnumerator];
CNCDirectoryWatcher *directory;
while (directory = [enumerator nextObject])
{
[directory update];
}
// release the pool
[pool release];
[lock unlock];
}
busy = NO;
}
And here are the mutator methods which can be called at any time by
other threads
- (void) addDirectory:(CNCDirectoryWatcher *) inObserver
{
[directories setObject: inObserver forKey: [inObserver directoryID]];
}
- (void) removeDirectory:(CNCDirectoryWatcher *) inObserver
{
[directories removeObjectForKey: [inObserver directoryID]];
}
How do I get other threads to go into a holding pattern until the
target thread is done enumerating the directories array? I want the
other threads to be able to add their items without losing any data,
just not until the target is done.
Any pointers appreciated
_______________________________________________
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