Brief introduction
Proper use of multithreaded programming can provide task execution efficiency and system resource utilization
- Multithreading is to improve resource utilization, and application responsiveness, multiple threads share application resources
- Each application has a main thread, which is usually used to refresh the UI interface, etc.
- More time-consuming tasks, if placed in the main thread, can clog the main thread, fail to respond to user actions, and typically create their own threads for time-consuming tasks and execute concurrently with the main thread
Multithreaded programming improves the utilization of system resources and the task processing speed to some extent, but the thread is not easy to be too much, otherwise it will cause the following problems.
- Too many threads can cause frequent scheduling of processors, and thread scheduling consumes a lot of system resources.
- Multiple threads in the same process share the resources of the process, and multiple threads accessing the resource need to be synchronized, and if too many threads increase the difficulty of synchronization, or even fail to implement.
How to implement multithreading in iOS development
Pthread
- Cross-platform for multiple operating systems with strong portability
- is a set of pure C language Common API, and the life cycle of the thread needs the programmer to manage, use more difficult, usually do not use
Nsthread
- The OC language-based API makes it easy to use, object-oriented operations
- The thread's declaration cycle is managed by programmers and is occasionally used
GCD
- C-based API to take full advantage of device multicore, designed to replace threading technology such as Nsthread
- The life cycle of a thread, automatically managed by the system, often used
Nsoperation
- Based on the OC language API, the bottom layer is GCD, which adds some easier-to-use features, using more object-oriented
- The thread life cycle is automatically managed by the system and is often used
Pthread
Creating Threads
Sets/Gets the number of concurrent executions of a thread
Perform a single task (typically used to design a singleton mode)
Define Tags:
Perform one-time tasks:
- int Pthread_once (pthread_once_t *, void (*) (void))
Convert thread state to unjoinable state to ensure the release of resources
- int Pthread_detach (pthread_t)
Exit thread
- void Pthread_exit (void *)
Nsthread
-
Thread life cycle operations
Other operations for Nsthread
Related to the main thread
- + (Nsthread *) Mainthread, get the main thread
- + (BOOL) Ismainthread, judging if the current thread is not the main path
Related to current thread
- + (Nsthread *) CurrentThread, gets the current thread
Determine the state of a thread
- \ Determines whether a thread is executing by using the executing property
- \ Determines whether a thread is finished by using the finished property
- \ Determines whether a thread is canceled by using the Cancelled property
Thread synchronization
- Cause: Multiple threads accessing the same resource are likely to cause data confusion and data security issues
- Solution: Use a mutex to resolve the mutex access problem, lock the critical resource (lock) {}, and usually use self as the lock
- Note: Because thread synchronization consumes a large amount of resources, you should try to avoid multiple threads accessing the same resource, and the logic of thread synchronization is usually referred to the server side for implementation
Communication between threads
- Returning from a child thread to the main thread
--(void) Performselectoronmainthread: (SEL) Aselector withobject: (ID) arg waituntildone: (BOOL) Wait modes: (Nsarray *) Array
-array Specifies the mode of Runloop, and if it is empty, aselector is not executed
-The caller of the method is the caller of the Aselector
--(void) Performselectoronmainthread: (SEL) Aselector withobject: (ID) arg waituntildone: (BOOL) wait
- From one thread to another (including the main thread)
--(void) Performselector: (SEL) aselector onthread: (nsthread ) THR Withobject: (ID) arg waituntildone: (BOOL) wait Modes: (Nsarray ) array
--(void) Performselector: (SEL) aselector onthread: (Nsthread *) THR Withobject: (ID) arg waituntildone: (BOOL) wait
- Communication can also be achieved by Nsport objects
Oc-19.pthread and Nsthread