Multithreading and GCD for IOS development, and multithreading gcd for ios
Thread execution:
Obviously, if multithreading is not enabled, the runA method is followed by runB before returning to runA, and then runC, A --> B --> C.
If runA is loading a network request (such as loading images), the main thread will be blocked. That is to say, the method of loading the image first and then following it. For example, runC is a click event, when the image is not loaded, the click event does not respond. How can this problem be solved? This requires multithreading.
Multi-thread execution
After multithreading is enabled, runB is used while runC is used, so that the main thread will not be rented.
Basic concepts of multithreading:
1. Each program has a main thread. When a program starts, it is created by calling the main function ).
2. the life cycle of the main thread is bound to the application. When the program exits, the main thread stops.
3. multithreading technology indicates that an application has multiple threads. using multithreading can provide CPU usage and prevent the main thread from blocking.
4. Any task that may block the main thread should not be executed in the main thread (such as the Execution network ).
Notes for Multithreading
1. The thread usage is not uncontrolled. The stack size of the main thread in iso is 1 MB and Kb from the second thread. These values cannot be changed through the compiler switch or the thread API function.
2. Only the main thread can directly modify the UI (for example, to set an image, you must first go back to the main thread to set it ).
Sub-thread memory management: needs to be added to the automatic release pool
Three multithreading technologies in IOS
1. pthread is the API of the POISIX thread and is a C language technology that can be used to directly operate the thread.
2. NSThread is a thread corresponding to each NSThread object. It is lightweight and abstract to pthread.
3. GCD (Grand Central Dispatch) is a C-language framework that uses queue management threads to make full use of multiple cores.
4. NSOperation/NSOperationQueue is an object-oriented thread technology that abstracts GCD.
Comparison of Three multithreading Technologies
I. NSThread
1. Advantages: NSThread is lightweight and easy to use than the other two.
2. Disadvantages: You need to manage the thread's life cycle, thread synchronization, locking, sleep, and wakeup on your own. Thread Synchronization will cause system overhead for locking the thread.
Ii. NSOperation
1. Don't worry about thread management or data synchronization. You can focus on the execution of operations you need.
2. NSOperation is object-oriented
Iii. GCD
1. It is a multi-core programming solution developed by Apple and can be used only after ios4.0. It is an alternative to the above two advanced and powerful technologies.
2. C language-based