objective-crunloop

Why RunLoop doesn't block the whole thread from executing?


If each UIApplication initializes a RunLoop like this on the main thread

void CFRunLoopRun(void) {   /* DOES CALLOUT */
    int32_t result;
    do {
        result = CFRunLoopRunSpecific(CFRunLoopGetCurrent(), kCFRunLoopDefaultMode, 1.0e10, false);
        CHECK_FOR_FORK();
    } while (kCFRunLoopRunStopped != result && kCFRunLoopRunFinished != result);
}

Why it doesn't block the whole execution since it is an infinite loop? How could an infinite loop and my code on the main thread work together on a single thread without context switching?


Solution

  • It does block the current thread. However, part of what CFRunLoopRunSpecific does is call your code on that thread. When you return, it goes back to CFRunLoopRunSpecific, which then calls other code.