I. Multi-threaded implementation scenarios
1>pthread
1) Language: C language
2) Features: (1) Universal multi-threaded API
(2) suitable for unix\linux\windows, etc.
(3) Cross-platform \ Portable
(4) very difficult to use
3) Life cycle: Programmer Management
Example:
-(void) Touchesbegan: (Nsset<uitouch *> *) touches withevent: (Uievent *)Event{ //1. Create a Pthread objectpthread_t thread; //2. Create a thread /*first parameter: Thread object, need to pass address the second argument: some properties of the thread, you can pass a null third argument: The method to invoke after the thread is created, note that this is a pointer to the function fourth argument: the third parameter (function pointer) received in the parameter, if there is no Then pass NULL*/Pthread_create (&thread, NULL, run, NULL); pthread_t thread2; pthread_t thread3; Pthread_create (&thread2, NULL, run, NULL); Pthread_create (&thread3, NULL, run, NULL);}void*run (void*param) {NSLog (@"--run--%@", [Nsthread CurrentThread]); returnNULL;}
2>nsthread
1) Language: OC language
2) Features: (1) can be used for thread state operation, easy to use
(2) Use more object-oriented
3) Life cycle: Programmer Management
Note the place:
1) Four ways to create Threads Alloc initwith..| detachnewthread|performselectorinback..| Custom
2) Set the thread's properties (name & Priority [0.0~1.0])
3) Nsthread The life cycle of the creation thread: Released when the task is completed
3>GCD (Task + queue)
1) Language: C language
2) Features: (1) can make full use of the device multicore, and is automatic
(2) Designed to replace threading technology such as Nsthread
3) Life cycle: Automatic management
Note the place:
1) Global concurrent queue, is the system comes with the queue, need to call, a total of four, their priority is the default. The official Apple document states that the fence function is not valid for them.
2) The home row, the tasks that are added to the main queue are executed in the main thread.
3) Use Step: Encapsulate task and drop task to queue, the system will process the task to corresponding thread according to FIFO principle
4) Sync function + Serial queue--no threads, all tasks executed serially
Sync function + Concurrent Queue--no threads, all tasks executed serially
Sync function + Home column--deadlock (because the Home Row Scheduler task checks the main thread state first, and if the main thread finds that it is busy pausing the schedule, this situation is deadlocked)
Async function + Serial queue--a thread is opened and all tasks are executed serially
Asynchronous functions + Concurrent queues--many threads are opened and all tasks are executed concurrently
Async function + Home Row--no threads, all tasks executed serially
5) before iOS6.0, creat once release once. iOS6.0 included in Arc management
6) Other common functions
① Fence Function--to control the order in which tasks are executed, but not with global concurrency queues (as Apple's official document shows)
② one-time function-the entire program runs, one-off code executes only once, can be used to implement a singleton mode (note, not put into lazy loading)
③ deferred execution
④ Fast Iteration (note that the primary queue cannot be used)
⑤ use of queue groups
4>nsopreation (Operation + queue)
1) Language: OC language
2) Features: (1) Its bottom is based on GCD, more than GCD a few simple and practical functions
(2) Use more object-oriented
3) Life cycle: Automatic management
Note the place:
1) nsoperation used for encapsulation operation, nsopreationqueue for storing tasks
2) Implementation steps: Encapsulate Operation---> Add operation to queue
3) for nsblockoperation, you can append a task, if there are multiple tasks in an operation, the child thread is opened, but not necessarily in the child thread
4) Custom Nsopreation---> Rewrite the internal Main method, which facilitates the encapsulation and reuse of code
5) The non-home row has both serial and concurrency features, which by default are concurrent queues that are implemented by controlling the maximum number of concurrency
6) The Start method is automatically called inside the method that adds the operation to the queue
7) The pending:yes--> of the queue is paused, no--> continues; (Hangs can only suspend tasks that are not currently in the execution state)
8) Cancellation of the queue: Cancel unrecoverable; (Apple's official recommendation is to check whether the current operation is canceled if it is a custom nsopreation, for each time elapsed)
9) The operation of the dependency and monitoring: can be cross-queue dependency, pay attention to not cycle of dependence on the good
2016-5-22 Niven Moore
A ramble on multithreading (II.)