Shallow into understanding gcd concurrent parallel Synchronous asynchronous multithreading

Source: Internet
Author: User
Tags gcd

What is gcd?!

GCD is a library of functions (nonsense)

Used to squeeze the resources of the system, to solve some problems in multi-threaded processing of the library (know this is enough, many movie characters are because they know too much to die badly!!!!! )

1. concurrency versus parallel Concurrency vs Parallelism

Single-core devices: Run a thread first, performing a context switch. This usually switches quickly enough to give us the illusion of a concurrent execution. That is, in a second eat a meal also pulled a excrement, but every half second can only do one thing (eat or poop) but the feeling seems to be at the same time. This is a single-core logic that simulates the illusion of two threads as shown in Concurrency ;

Multi-core device: executes multiple threads concurrently. That is, at the same time do two things as I have two wives (CPU), one to give me a pinch back, a feed me to eat the grape double enjoy at the same time, this will not be only a wife her task is always switch to get tired of dying.

Mutual exclusion: The use of critical resources between processes that are mutually exclusive. With the above two wife's example explanation, is to pinch back to want to feed you eat grapes, but you have only one mouth.

2. Synchronous and asynchronous synchronous vs asynchronous

Synchronization: Synchronization is the sequential execution of the next execution, the task and the task have an interdependent relationship, that is, the output of the previous process as the input of the latter process, when the first process does not output the second process must wait. That is, the naked bathing, no hot water, except the naked body silly and so hot water can do nothing. A group of concurrent processes that have synchronization relationships send each other information called messages or events. Continue to the example above, that is, to take a shower of clothes, to hot water one thing, stripped clothes you tell the boiler to hot water called messages or events.

Async: Async is independent of each other, while waiting for an event to continue to do its own thing, do not have to wait for this event to complete and then work. A thread is a way to implement Asynchrony. Async is the main thread that lets the calling method do not need to wait for another thread to finish synchronously, so that the main thread can Cheng Gan other things. Saying so much is one hand (thread) the other hand (multithreading) The thing that takes toilet paper.
Async and multithreading are not an equal relationship, asynchronous is the ultimate goal, multithreading is just a means for us to implement Asynchrony. Asynchronous is when a call request is sent to the callee, and the caller can do something else without waiting for the return of the result. The implementation of Asynchrony can be handled by multithreading or by handing it to another process. This one's own evil understanding.

3.Serial Queues Serial Queue

Executes only one task at a time, and executes in the order that we added to the queue, FIFO first-out.

4.Concurrent Queues Concurrent Queue

The tasks in the concurrent queues are guaranteed to be executed in the order in which they were added, but the tasks may be completed in any order, and you will not know when to start the next task, or how many blocks are running at any one time. It all depends on the GCD. Block 0 1 2 3 is the addition order, but because the content of the processing different system allocates different resources to deal with, so the time of completion is different , processing some slow end time this is not the ape can control. However, GCD provides (queueTypes queue type) a queuing priority to ensure as much as possible, in fact, there is no guarantee, but there is always better than not.

Note that after block 0 starts, other tasks wait for block 0 to complete before starting.  Block 3 is a bit more than Block 2, but it is done before Block 2. When to start a Block depends entirely on GCD. If the execution time of one Block overlaps another, it is up to the GCD to decide whether to run it on a different core, or, if that core is available, to use the context switch to execute the different blocks. It is said that the use of GCD do not consider multi-core issues, the system to help you consider.  

Shallow into understanding gcd concurrent parallel Synchronous asynchronous multithreading

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.