Re: GCD dispatch workers and termination
Re: GCD dispatch workers and termination
- Subject: Re: GCD dispatch workers and termination
- From: Ken Thomases <email@hidden>
- Date: Sun, 27 Mar 2011 16:14:46 -0500
On Mar 27, 2011, at 1:27 PM, Jason Harris wrote:
> - (void) doWorkerLaunches
> {
> for (int i = 1; i <50; i++)
> {
> dispatch_async(dispatch_get_global_queue(0,0), ^{
> NSTask* task = [[NSTask alloc] init];
> [task setLaunchPath: @"/bin/ls"];
>
> NSArray* arguments = [NSArray arrayWithObjects: @"-l", @"-a", @"-t", nil];
> [task setArguments: arguments];
>
> NSPipe* pipe = [NSPipe pipe];
> [task setStandardOutput: pipe];
>
> NSFileHandle* file = [pipe fileHandleForReading];
>
> [task launch];
> [task waitUntilExit];
>
> NSData* data = [file readDataToEndOfFile];
> });
> }
> }
Possibly unrelated to your issue with the dispatch threads (although possibly related), the above use of NSTask is somewhat broken. You've made a common mistake. It is not OK to block waiting for the task to exit when you haven't established an ongoing asynchronous read of its output (when you're capturing the output rather than letting it go to file, /dev/console, or /dev/null, etc.).
The problem is that pipes have a fixed buffer in the kernel. If a task writes more than that amount to the pipe when no reader is draining the pipe, the writer blocks. You don't read from the pipe until after the task has exited, but the task may be prevented from exiting because you're not reading from the pipe. Classic deadlock.
Put another way, have you confirmed that your tasks are really completing? Maybe the dispatch threads are still alive because of this deadlock I'm describing. (I guess it depends on whether your "ls -l -a -t" command is producing more output than the size of a pipe's buffer, which in turn depends on the current working directory and its contents.)
Regards,
Ken
_______________________________________________
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