IOS Multithreading-Threading Basics

Source: Internet
Author: User

Most modern operating systems, including iOS, support the concept of thread execution. Each process can contain multiple threads, and they can run concurrently. If there is only one processor core, the operating system switches between all the execution threads, much like switching between all the execution threads. If you have multiple cores (cores), the threads will be dispersed across multiple cores like a process.

All threads in a process share executable program code and global data. Each thread can also have some unique data. A thread can use a special structure that becomes a mutex or a lock, which ensures that a particular block of code cannot be executed at a time without multiple threads. When multiple threads access the same data at the same time, this helps to ensure the correct results, locking other threads when one thread updates a value (called a critical section in code).

We typically focus on thread safety (THREAD-SAFE) issues while processing threads. Some repositories consider thread concurrency when they are written, and use mutexes to properly protect all their critical sections. There are also some code libraries that are not thread-safe.

For example,in iOS Cocoa Touch, the foundation framework (which contains basic classes for all OBJECT-C programming types, such as NSString, Nsarray, and so on) is generally considered thread-safe . However, theUikit framework (including classes that specialize in building GUI applications, such as UIApplication, UIView, and all of its subclasses) is non-thread-safe . This means that in a running iOS app, all method calls that process any Uikit object should be executed from the same thread, which is called the main thread-mainthread. If you access the Uikit object from another thread, the result is a lot of unexpected results (errors)! -- So the update UI runs on the main thread.

By default, the main thread performs all the actions of the iOS app (such as handling user time-triggered actions), so there is nothing to worry about for a simple application, and the user-triggered action is already (the default) running in the main thread.

IOS Multithreading-Threading Basics

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.