iOS Development threads and Runloop

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.