title: In Apple's Cocoa API, why is it important that NSApplicationMain be called from the main thread?
body: <p>In the documentation for <a href="http://developer.apple.com/library/mac/#documentation/Cocoa/Reference/ApplicationKit/Miscellaneous/AppKit_Functions/Reference/reference.html#//apple_ref/c/func/NSApplicationMain" rel="noreferrer">NSApplicationMain</a>, it says:</p>
  
  <blockquote>
    <p>Creates the application, loads the main nib file from the application’s main bundle, and runs the application. You must call this function from the main thread of your application [...].</p>
  </blockquote>
  
  <p>The "main thread" obviously refers to the first thread of the program, where <code>main(argc, argv)</code> starts. A quick look through the <a href="http://developer.apple.com/library/IOS/#documentation/Cocoa/Reference/Foundation/Classes/NSThread_Class/Reference/Reference.html" rel="noreferrer">NSThread</a> documentation reveals <code>+ (BOOL)isMainThread</code>, which can be used to determine whether the current thread is the "main" one or not. I ran some tests: this method works regardless of whether <code>NSApplicationMain</code> has been called yet.</p>
  
  <p>My question has two (somewhat related) parts:</p>
  
  <ol>
  <li>What is so special about the main thread for <code>NSApplicationMain</code>?</li>
  <li>How does Cocoa identify the main thread in the first place?</li>
  </ol>