• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: Challenge: Block Main Thread while Work is done, with Timeout
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Challenge: Block Main Thread while Work is done, with Timeout


  • Subject: Re: Challenge: Block Main Thread while Work is done, with Timeout
  • From: Ken Thomases <email@hidden>
  • Date: Fri, 24 Jul 2009 22:33:05 -0500

On Jul 24, 2009, at 5:10 PM, Dave Keck wrote:

The notifications don't cross threads; they are delivered in the thread
where they are posted.

Yes, I understand that. There must be some quirk that allows it to work
when doing an NSTask

Just a tidbit - I don't think there's any quirks involved. The general idea is this: NSTask creates a run loop source on the main thread, and spawns a second thread to watch the process for termination. When the process terminates, the secondary thread signals the main thread's run loop (via CFRunLoopSourceSignal()), and the run loop source's callback then executes on the main thread, which issues the notification. On 10.5+, this can be done trivially using kqueue and CFFileDescriptor.

First, I believe that NSTask installs a run loop source in the thread which launches the task, not necessarily the main thread. This often bytes people who launch an NSTask from a secondary thread, but never let that thread's run loop run, so they never receive notification that is has terminated. Also, there are reports that some NSTask resources don't get cleaned up if the thread it was launched from is allowed to exit before termination, again presumably because the run loop wasn't run to handle the event of the termination.


Second, there's no need for NSTask to spawn a second thread to monitor the process for termination. The innards of NSTask are of course an implementation detail. That said, if it's using kqueue to monitor the process or using low-level Mach mechanisms (there are notifications that a Mach port has been destroyed/orphaned which can be used to learn when the owning process has terminated), that can be done with the run loop and the source that's installed. The whole point of run loops is to be able to monitor various things _without_ spawning a thread for each.

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


References: 
 >Challenge: Block Main Thread while Work is done, with Timeout (From: Jerry Krinock <email@hidden>)
 >Re: Challenge: Block Main Thread while Work is done, with Timeout (From: Ken Thomases <email@hidden>)
 >Re: Challenge: Block Main Thread while Work is done, with Timeout (From: Jerry Krinock <email@hidden>)
 >Re: Challenge: Block Main Thread while Work is done, with Timeout (From: Dave Keck <email@hidden>)

  • Prev by Date: Re: Crash when MyDocument becomes the delegate of NSApplication
  • Next by Date: Re: Challenge: Block Main Thread while Work is done, with Timeout
  • Previous by thread: Re: Challenge: Block Main Thread while Work is done, with Timeout
  • Next by thread: Re: Challenge: Block Main Thread while Work is done, with Timeout
  • Index(es):
    • Date
    • Thread