1

From this Apple documentation:

https://developer.apple.com/library/mac/documentation/Cocoa/Conceptual/Multithreading/RunLoopManagement/RunLoopManagement.html#//apple_ref/doc/uid/10000057i-CH16-SW1 ,

Run loops are part of the fundamental infrastructure associated with threads. A run loop is an event processing loop that you use to schedule work and coordinate the receipt of incoming events

When we use Cocoa APIs, does all of the code run in a runloop?

bneely
  • 9,083
  • 4
  • 38
  • 46
Lee xw
  • 133
  • 9

2 Answers2

4

Each thread, including the application’s main thread, has an associated run loop object. The app frameworks automatically set up and run the run loop on the main thread as part of the application startup process.

The answer to your question is YES and NO. It is a matter of interpretation.

An NSRunLoop object processes input for sources such as mouse and keyboard events from the window system, NSPort objects, and NSConnection objects. An NSRunLoop object also processes NSTimer events.

A run loop delivers the events to the thread, events that can be handled by the thread. Thus the code you write will handle these events. Input sources deliver asynchronous events, usually messages from another thread. Timer sources deliver scheduled events, that are either repeated or delivered at a particular time.

enter image description here

In a big simplification you can think of a run loop like of a while loop:

while(1)
{
    checkInputSourcesAndInformThreadIfNeeded();
}

But a run loop does not only that. It can also schedule methods for execution and prioritize them (see performSelector:target:argument:order:modes: method).

Moreover, it can generate notifications about its behavior. Other objects can register as observers to get notified about run loop events.

Based on Threading Programming Guide and NSRunLoop Class Reference.

Related SO thread: How to implement an NSRunLoop inside an NSOperation

Community
  • 1
  • 1
Rafał Sroka
  • 39,540
  • 23
  • 113
  • 143
  • No interpretation about it; the code either runs in a run loop because it is the main thread or you started a run loop or it does not run in a run loop. – bbum Mar 02 '14 at 01:49
0

main doesn't. Code in worker threads doesn't. Signal handlers may or may not.

Seva Alekseyev
  • 59,826
  • 25
  • 160
  • 281
  • The main thread in a cocoa/ios app always has a run loop. Worker threads may or may not. Signal handlers -- as in signal() -- are dysfunctional. – bbum Mar 02 '14 at 01:50
  • `main` itself gets control before the run loop starts. – Seva Alekseyev Mar 02 '14 at 02:17
  • ... and the very first thing `main` does is call `NSApplicationMain()` or `UIApplicationMain()`, which start the main event loop. It is not altogether unheard of to schedule a timer and/or run loop source in `main` exactly because no Cocoa/iOS app will ever run without a run loop on the main thread. (Yes, a command line program won't require an MEL, but the OP was asking about a Cocoa app). – bbum Mar 02 '14 at 07:03
  • Exactly. Which answers the OP's question in the negative: "does all of the code run in a runloop"? No, the code that *starts* the run loop runs, by definition, outside of it. Also, you're free to make your `main` as involved as you want. It's just a C function in your program. – Seva Alekseyev Mar 02 '14 at 15:59