In general, a thread can execute only one task at a time, and the thread exits after execution, and if we need a mechanism to allow the thread to handle the time at all times without exiting, the usual code logic is this:
This is the Event loop framework.
Runloop is actually an object that manages the events and messages it needs to handle, and provides an entry function to execute the logic of the event loop above. After the thread executes this function, it is in the loop of "accept message-wait-process" inside this function, knowing that the loop ends (for example, an incoming quite message) and the function returns.
In OSX and iOS, two such objects are available: Nsrunloop and Cfrunloopref.
Cfrunloopref is based on the Corefoundation framework, which provides APIs for pure C functions, all of which are thread-safe.
Nsrunloop is a cfrunloopref-based package that provides an object-oriented API, but these APIs are not thread-safe.
Runloop and threading Relationships: iOS development has two thread objects-pthread_t and Nsthread. The Pthread_tnsthread is one by one corresponding. For example, you can get the current thread by PTHREA_MAIN_THREAD_NP () or [Nsthread CurrentThread]. Cfrunloop is managed based on Pthread.
Apple does not allow the creation of Runloop directly, but provides two automatically acquired functions: Cfrunloopgetmain () and Cgrunloopgetcurrent (). The internal logic of these two functions is presumably:
The thread and Runloop are one by one corresponding, and their relationship is stored in a global dictionary. The line Cheng Gang is created without runloop, and if you do not actively acquire it, it will never be there. The creation of runloop occurs at the time of the first acquisition, when the thread ends at the time of the destruction. You can only get its runloop inside a thread (except for the main threads).
The event driver body is a dead loop, nothing-hibernation, something-wake-up-execute
Runloop: For endings
1. Keep the program running and accept user input
2. Decide when the program should handle which event
3. Call decoupling (Message Queue)
4. Save CPU Time
iOS Development threads and Runloop