• 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: Detecting if UI thread is running, and creating a UI thread
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Detecting if UI thread is running, and creating a UI thread


  • Subject: Re: Detecting if UI thread is running, and creating a UI thread
  • From: James Bucanek <email@hidden>
  • Date: Fri, 2 Mar 2007 11:19:54 -0700

Justin Armstrong wrote on Friday, March 2, 2007:
>By "UI thread" i just mean the thread which runs the event loop.  This
>seems to be called the "main thread" in Mac documentation, so i'm
>wondering if this means that it is hardwired to be the first thread of
>the process.

Justin,

I fear you are confusing yourself with assumptions.

- A process has at a minimum one thread, and you can spawn more.

- A run loop can be started in any thread.

- The standard Carbon/Cocoa application framework starts one run loop for you, which is designated as the "main" run loop. By extension, the thread that is running the main run loop is referred to as the main thread.

>Obviously a standard Cocoa/Carbon app will run the event
>loop in its first thread.

I starts a run loop in *a* thread and designates that the main run loop. It's immaterial whether it's the first thread or not.

>In the case of a Cocoa app the event loop is
>started by the call to NSApplicationMain() from the boilerplate main()
>function.

Correct.

>What I'd like to know is if it is possible for a command line
>type program which has not started an event loop to spawn a second
>thread and then make that the "main thread".  Is it just a case of
>calling NSApplicationMain in the new thread instead?

A command line tool can start multiple threads, and any of those threads can run a run loop. I do this in background deamons and tools that need to communciate with other processes via distributed objects (all distributed object communications is handled via run loop queues). Works great.

You seem to want a "main" run loop from which you can do UI stuff. The problem is not starting a new thread or starting a run loop in that thread. The problem is that applications (which do UI stuff) rely on much more than just a thread and a run loop to work. Command-line applications simply don't have those resources/connections.

If you want to do UI stuff you need to create an application, which means it needs to be started as an application and have all of the required resoureces (bundle, info.plist, ...). You can't turn a command-line tool into a GUI application simply by starting a run loop.

--
James Bucanek
_______________________________________________

Cocoa-dev mailing list (email@hidden)

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: 
 >Re: Detecting if UI thread is running, and creating a UI thread (From: Justin Armstrong <email@hidden>)

  • Prev by Date: Re: Help needed with saving Core Data app data
  • Next by Date: Re: Help needed with saving Core Data app data
  • Previous by thread: Re: Detecting if UI thread is running, and creating a UI thread
  • Next by thread: Re: Detecting if UI thread is running, and creating a UI thread
  • Index(es):
    • Date
    • Thread