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?
Is there a way to determine on which thread a specified piece of code is being run?
views:
49answers:
1
+1
A:
All method calls take place in the current thread, unless you do something like
performSelectorInBackground:
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
2010-04-16 05:08:49
(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
2010-04-16 05:41:00
See also [NSThread currentThread] and [NSThread mainThread].
JeremyP
2010-04-16 15:08:58