preface

First through thread and process definition and distinction introduced thread definition and use skills in daily development.

Thread defined

Thread is the basic execution unit of a process. All tasks of a process are executed in the thread. If a process wants to execute tasks, it must have a thread, and the process must have at least one thread.

The process definition

A process is an application that is running in the system. Each process is independent and runs in its dedicated and protected memory space. You can view the running process in the Mac system using the Activity Monitor

The relationship between processes and threads

Address space: Threads of the same process share the address space of the same process, while processes are independent of each other.

Resource ownership: Threads in the same process share the resources of the same process, such as memory, I/O, and CPU, but resources between processes are independent.

1: a process crash in protected mode has no impact on other processes, but a thread crash kills the entire process. So multi-processing is more robust than multi-threading.

2: Process switchover consumes large resources and achieves high efficiency. So when it comes to frequent switching, threads are better than progressions. Also, if you require concurrent operations that share variables at the same time, use threads instead of processes

3: Execution process: Each independent process has a program run entry, sequential execution sequence and program entry. However, threads cannot execute independently and must depend on the application, which provides multiple thread execution control.

Thread is the basic unit of processor scheduling, but process is not. 5: Threads have no address space. Threads are contained in the process address space

Advantages of threads

advantages

  • Can improve the execution efficiency of the program
  • Increase resource utilization (CPU, memory)
  • The task on the thread is automatically destroyed after it completes execution

* weakness

  • Starting threads takes up a certain amount of memory (512 KB for each thread by default) * If you start a large number of threads, it takes up a large amount of memory and reduces the performance of your program
  • The more threads there are, the more overhead the CPU has on calling threads
  • The program design is more complex, such as communication between threads, multithreaded data sharing

Multithreaded application

1. Pthread A set of generic multithreading apis. Applicable to Unix/Linux/Windows and other systems, cross-platform, portable, difficult to use 2.NSThread is more object-oriented. Simple and easy to use, can directly operate thread objects 3.GCD is designed to replace NSThread and other thread technology, make full use of equipment and other multi-core 4

The life cycle of a thread

Application of threads

  1. The mutex

    Ensure that the code in the lock can be executed by only one thread at a time!

    The lock range of mutex should be as small as possible. The larger the lock range, the worse the efficiency!

  • Mutex parameter
  • Any NSObject object that can be locked
  • Note: The lock object must be accessible to all threads
  • If there is only one place in the code that needs to be locked, use self most of the time to avoid creating a separate lock object
  1. Atomic ` ` nonatomicThe difference between

Atomic atomic properties (thread-safe), designed for multithreading, default.

It is guaranteed that only one thread can write at a time (but multiple threads can write at the same time). Atomic has a lock of its own (spin lock)

Atomic: Thread-safe, consuming a lot of resources

Nonatomic: Non-thread-safe, suitable for mobile devices with small memory

IOS development tips

All attributes are declared as nonatomic and try to avoid multiple threads snatching the same resource. Try to hand over the business logic of locking and resource snatching to the server to reduce the pressure of mobile clients

  1. Thread and Runloop relationship

    Runloops correspond to threads one by one. A runloop corresponds to a core thread, which is called core because runloops can be nested, but there can only be one core. Their relationship is stored in a global dictionary.

    2: The runloop is used to manage threads. When the runloop is enabled, threads will go to sleep after executing tasks and will be woken up to execute tasks.

    3: Runloop is created on the first fetch and destroyed at thread end.

    4: For the main thread, the runloop is created by default as soon as the program starts.

    5: For child threads, runloops are lazily loaded and only created when we use them, so be careful when using timers on child threads: make sure the runloop is created for the child thread, otherwise the timer will not call back.

Thread locking methods are as follows: mutex, spin lock

  1. The lock implemented by NSLock.
  2. A lock built using the synchronized keyword
  3. Use C language pthread_mutex_t implementation of the lock

Os_unfail_lock 5.OSSpinLock (supported only by iOS10) 6.dispatch_semaphore (supported by ios9) 7.pthread_mutex recommended NSCondition 10. Pthread_mutex (recursive) // recursive lock 11.NSRecursiveLock 12.NSConditionLock 13.@synchronnized

Multithreaded application

In daily development, multi-threaded applications such as GCD and OperationQueue are highlighted in the next article.