views:

49

answers:

1
  1. If I spawn a secondary thread and the threaded method calls other methods, are those methods run in the secondary thread or the main thread?

  2. Is there a way to determine on which thread a specified piece of code is being run?

+1  A: 
  1. All method calls take place in the current thread, unless you do something like performSelectorInBackground:

  2. You can probably tell if you're running in the main thread by comparing [NSRunLoop currentRunLoop] to [NSRunLoop mainRunLoop]

See also: Threading Programming Guide

David Gelhar
(1), alternately stated: All messages to objects are received on the same thread they're sent from. Nothing creates a thread unless it explicitly creates a thread. (“Documented as creating a thread” counts as “explicitly creates a thread.”) (2): Or send `[NSThread isMainThread]`. However, the program should not need to test whether it is running on the main thread. Any given piece of code should either (a) only ever run on (the main|a secondary) thread or (b) not care what thread it's running on.
Peter Hosey
See also [NSThread currentThread] and [NSThread mainThread].
JeremyP