iOS development-Understanding and understanding of threading and processes

Source: Internet
Author: User
Tags gcd

Process:
    • A process is a running application in the system, such as the simultaneous open and Xcode, the system will start 2 processes respectively;
    • Each process is independent, and each process runs within its dedicated and protected memory space;
Threads:
    • To perform a task, a process must be wired (at least one thread per process), the smallest unit of execution in the process, an entity in the process, and the basic unit of Dispatch and dispatch independently by the system;
    • All tasks of a process (program) are executed in a thread;
    • A program has only one main thread, the program is created at startup (call Main to start), the life cycle of the main thread is bound to the application, the program exits, the main thread also stops;
    • At the same time, a thread can perform only one task, and to perform multiple tasks in 1 processes, these tasks can only be performed sequentially (serial threads);
    • The thread itself does not own system resources and only has the resources necessary to run, but it can share all the resources owned by the process with other threads belonging to one process;
several states of a thread:
    • New state: A newly created thread object;
    • Ready state: After the thread object is created, other threads call the object's Start method, which is in a running thread pool and becomes operational, waiting for the CPU to be used;
    • Running state: The ready state of the thread gets the CPU, executes the program code;
    • Blocking state: For some reason to abandon the use of the CPU, suspend operation, know that the thread is ready to go to the state of operation;
    • Dead state: The thread has finished executing or exited the Run method due to an exception, the end of the thread life cycle;
process and Thread comparisons:
    • A thread is the smallest unit of CPU scheduling (executing a task) and is the smallest unit of program execution.
    • Process is the unit of CPU allocating resources and dispatching;
    • A program can correspond to multiple processes, a process can have multiple threads, but there must be at least one thread, and a thread can only belong to one process;
    • Threads in the same process share all the resources of the process;
Multithreading:
    • Concept: Multiple threads can be opened in a process, and each thread can perform different tasks in parallel (at the same time);
    • Principle: At the same time, the CPU can only process a thread, only one thread in the work, multi-threaded concurrency (at the same time) execution, in fact, the CPU is quickly dispatched between multiple threads (switching), if the CPU scheduler thread time is fast enough, resulting in multi-threaded concurrent execution of the illusion;
    • Note: If there are many threads, the CPU will be dispatched between N multithreading, consuming a lot of CPU resources, and the frequency of each thread being scheduled to execute will decrease (the execution efficiency of the thread will decrease);
advantages and disadvantages of multi-threading:
    • Advantages: Can improve the execution efficiency of the program and resource utilization (CPU, memory utilization) properly
    • Cons: Creating threads is expensive, the main cost of iOS includes: Kernel data structure (approximately 1kb), stack space (sub-thread 512kb, main thread 1MB), creation thread takes approximately 90 milliseconds to create time, if a large number of threads are turned on, it will degrade the performance of the program (typically up to 3 to 5); more Threads , the higher the CPU overhead on the scheduling thread; more complex programming (e.g., communication between threads, multi-threaded data sharing)
Main thread:
    • After an iOS program runs, 1 threads are turned on by default, called the "main thread" or "UI thread"
    • Function: Display/Refresh UI interface, handle UI events (click events, scroll events, drag events)
    • Use note: Do not put time-consuming operations into the main thread, time-consuming operations should be placed on child threads (background threads, non-main thread); General UI-related actions should be placed in the main thread
Multi-threading implementations in iOS:
    • Pthread: A universal multi-threaded API for UNIX, Linux, Windows and other systems, cross-platform, portable, difficult to use, C language, thread life cycle by the programmer management
    • Nstread:oc language, object-oriented, easy-to-use, direct manipulation of thread objects, thread lifecycles managed by programmers
    • GCD: (Common) alternative threading technology such as Nstread, take full advantage of device multicore, thread life cycle automatic management, C language
    • Nsoperation: (Common) The bottom is gcd, more than gcd more simple and practical functions, using more object-oriented, automatic management of thread life cycle

The following article will focus on the implementation of multithreading, the purpose of this article is to collate knowledge about multithreading, deepen the understanding and understanding of multithreading, if there is an improper understanding of the place, welcome to communicate and correct!

iOS development-Understanding and understanding of threading and processes

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.