Multi-thread GCD

Source: Internet
Author: User
Tags gcd

    • What is GCD

The Grand Central Dispatch is a multi-core programming technology developed by Apple. Mainly used to optimize applications to support multicore processors and other multi-symmetric processing systems TA will automatically manage the life cycle of threads (create threads, dispatch tasks, destroy threads) without our management at all, we just need to tell what to do. It also uses the C language, but the use of blocks (called closures in swift) makes it easier and more flexible to use. So basically everybody uses GCD this package

    • What are tasks and queues

There are two very important concepts in GCD that are tasks and queues

Task: Task is a function code, in GCD is generally a block or function (block with more), the task has two ways of execution, synchronous and asynchronous.

The difference between synchronous and asynchronous is whether the current thread is blocked:

If it is a sync operation, it blocks the current thread and waits for the task in the block to complete before the current thread continues to run down.

In the case of an asynchronous (async) operation, the current thread executes directly down, and it does not block the current thread.

The synchronization function cannot turn on the child thread and the async function has the ability to turn on the child thread.

Queues: For holding tasks, queues are generally divided into two types, serial queue (Serialqueue) and parallel queue (Concurrent), all of which follow FIFO.

The tasks in the queue have different execution modes depending on the synchronization and async:

  

Synchronous parallel, because the synchronization does not turn on the function of the child thread, so the parallel queue also loses the effect of parallelism, the most commonly used method is asynchronous parallel.

  

Main queue: This is a special queue, and the Home column is a queue in the main thread, so neither an async function nor a synchronous function generates a new branch thread, which means that the asynchronous function loses its ability to open the thread in the main queue. TA is used to refresh the UI, and any work that needs to refresh the UI is performed in the main queue, so the usual time-consuming tasks are put on to other threads to execute.

 

    • Use of GCD

1. Asynchronous concurrency

//1, asynchronous concurrency (most commonly used) to open a multi-threaded thread, the task is executed concurrently- (void) Asynglobal {//get the global concurrent queue//The first parameter represents the queue priority, and if the priority is high, the queue is dispatched more times. Typically, this priority is given to the default priority leveldispatch_queue_t queue = Dispatch_get_global_queue (Dispatch_queue_priority_default,0); //in general, parallel queues use this global concurrency queue, and do not want to use a global queue can also be created, because it is a C function, so the first parameter of the string is not easily preceded by an @ symbol//dispatch_queue_t queue = dispatch_queue_create ("6666", dispatch_queue_concurrent); //to add a task to a concurrent queue for asynchronous executionDispatch_async (Queue, ^{NSLog (@"%@-----You 484 stupid-----%d", [Nsthread CurrentThread], [Nsthread ismainthread]);    }); Dispatch_async (Queue,^{NSLog (@"%@------You didn't take your meds----%d", [Nsthread CurrentThread], [Nsthread ismainthread]);    }); Dispatch_async (Queue,^{NSLog (@"%@-----can take the pills-----%d", [Nsthread CurrentThread], [Nsthread ismainthread]);    }); Dispatch_async (Queue,^{NSLog (@"%@-----Take the pills-----%d", [Nsthread CurrentThread], [Nsthread ismainthread]);    }); Dispatch_async (Queue,^{NSLog (@"%@-----Street-----%d", [Nsthread CurrentThread], [Nsthread ismainthread]);    }); Dispatch_async (Queue,^{NSLog (@"%@-----Again on the street-----%d", [Nsthread CurrentThread], [Nsthread ismainthread]);        }); //if it is an MRC environment, the queue needs to be freed after it has been used//dispatch_release (queue);}

Execution effect If there are too many tasks to join the queue, in order not to consume too much performance, the system will control the number of threads that are turned on, and when the task is finished and the remaining tasks continue to parallel, then there will be the result of the same thread as the first and last. The value of [Nsthread Ismainthread] is 0 because it is an async function that turns on a child thread that is not in the main thread.
  

2. Asynchronous serial

//asynchronous serial (sometimes used) to open a threaded thread, and the task executes in one of the open threads- (void) asynserial {//Create a serial queuedispatch_queue_t queue = Dispatch_queue_create ("Bada.queue", dispatch_queue_serial); //to add a task to a serial queue asynchronous executionDispatch_async (Queue, ^{NSLog (@"%@-----You 484 stupid-----%d", [Nsthread CurrentThread], [Nsthread ismainthread]);    }); Dispatch_async (Queue,^{NSLog (@"%@------You didn't take your meds----%d", [Nsthread CurrentThread], [Nsthread ismainthread]);    }); Dispatch_async (Queue,^{NSLog (@"%@-----can take the pills-----%d", [Nsthread CurrentThread], [Nsthread ismainthread]);    }); Dispatch_async (Queue,^{NSLog (@"%@-----Take the pills-----%d", [Nsthread CurrentThread], [Nsthread ismainthread]);    }); Dispatch_async (Queue,^{NSLog (@"%@-----Street-----%d", [Nsthread CurrentThread], [Nsthread ismainthread]);    }); }
It can be seen from the result that it was executed on the same thread.
  

  

3. Synchronous concurrency

//synchronous concurrency (basic not used) if it is synchronous, the effect of concurrency disappears when the child thread is not turned on- (void) Synglobal {//get the global concurrent queuedispatch_queue_t queue = Dispatch_get_global_queue (Dispatch_queue_priority_default,0); //To add a task to a concurrent queue for synchronous executionDispatch_sync (Queue, ^{NSLog (@"%@-----You 484 stupid-----%d", [Nsthread CurrentThread], [Nsthread ismainthread]);    }); Dispatch_sync (Queue,^{NSLog (@"%@-----You didn't take your meds-----%d", [Nsthread CurrentThread], [Nsthread ismainthread]);    }); Dispatch_sync (Queue,^{NSLog (@"%@-----can take the pills-----%d", [Nsthread CurrentThread], [Nsthread ismainthread]);    }); Dispatch_sync (Queue,^{NSLog (@"%@-----Take the pills-----%d", [Nsthread CurrentThread], [Nsthread ismainthread]);    }); Dispatch_sync (Queue,^{NSLog (@"%@-----Street----%d", [Nsthread CurrentThread], [Nsthread ismainthread]);    }); }
  From the printing results can be seen, because it is synchronous, and does not open the sub-thread, are executed in the main thread, and the effect of concurrency has gone
  

Temporary knot ....

Multi-thread GCD

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.