Skip navigation.
 
mlhow do I make NSTask thread-safe?
FROM : Alex Reynolds
DATE : Wed Nov 27 19:10:37 2002

I have a problem where a program is freezing not on running an NSTask 
but when the NSTask reaches the method -waitUntilExit.

My code waits for a notification that disks have been ejected from 
NSWorkspace.

Then the code acquires a NSConditionLock, loops through a scan of the 
remaining mounted disks against an internal NSMutableArray, and if one 
is found to be in the mutable array but not "on the Desktop", a 
NSTask-based method is called to unmount a hidden disk.

Once the hidden disk is ejected, the conditional lock is released and 
the loop goes through its remaining iterations, until another mismatch 
is found and the NSTask is called again.

Something like this:

for (i = 0; i < number of disks in internal table; i++)
   [acquireConditionalLock]
   [searchInternalDiskTableAgainstNSWorkspaceMountedVolumes];
   if (mismatchFound)
       [callNSTaskToUnmountHiddenDiskThatIsLeftBehind];
   [releaseConditionalLock]

But what is happening is that the program hangs up on the portion of 
code in "callNSTaskToUnmountHiddenDiskThatIsLeftBehind" where I run 
[NSTask waitUntilExit].

I've been reading the multithreading documentation at:

Developer/Documentation/Cocoa/TasksAndConcepts/ProgrammingTopics/
Multithreading/

which says that NSTask is not thread-safe.

I wrapped the "callNSTaskToUnmountHiddenDiskThatIsLeftBehind" code in 
an NSLock, but this did not help. E.g.:

    NSLock    *unmountImageLock = [[NSLock alloc] init];
    [unmountImageLock lock];

    // hdiutil detach /dev/diskN
    NSTask     *hdiutilPrimaryDiskTask = [[NSTask alloc] init];

    [hdiutilPrimaryDiskTask setLaunchPath:@"/usr/bin/hdiutil"];

    // set arguments
    [hdiutilPrimaryDiskTask setArguments:[NSArray 
arrayWithObjects:@"detach",imageStubPath,nil]];
    [hdiutilPrimaryDiskTask launch];
    [hdiutilPrimaryDiskTask waitUntilExit];

    [unmountImageLock unlock];

Does anyone know how to make NSTask work with one thread at a time?

-Alex
_______________________________________________
cocoa-dev mailing list | <email_removed>
Help/Unsubscribe/Archives: http://www.lists.apple.com/mailman/listinfo/cocoa-dev
Do not post admin requests to the list. They will be ignored.

Related mailsAuthorDate
No related mails found.