https://khorbushko.github.io/article/2020/11/29/runloop-in-depth.html
RunLoop is the implementation of well-known EventLoop pattern - * programming construct or design pattern that waits for and dispatches events or messages in a program*.
while (!end) { }
This pattern has been implemented on many platforms. Thus, the main problems that it should resolve are:
- receive events/messages
- work when works exist and sleep when no work available (correct resource management).
Hight level description of Thread:
Hight level description of EventLoop:
iOS/macOS RunLoop
Talking about iOS/macOS we always refer to RunLoop. To be more correct - 2 classes implement this behavior:
CFRunLoopRef(open source)NSRunLoop(based onCFRunLoopRef)
As you already see, RunLoop is connected to the thread. You can’t create RunLoop directly, instead, it’s can be created at the very start of Thread creating and destroyed at the very end of theThread lifecycle. There are 2 function that provide access to RunLoop - CFRunLoopGetMain()and CFRunLoopGetCurrent()
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. The purpose of a run loop is to keep your thread busy when there is work to do and put your thread to sleep when there is none. - Apple.