Basic use of iOS multi-thread 5.GCD

Source: Internet
Author: User
Tags gcd

I have introduced several basic concepts of GCD in the previous article, but you may feel that there is no egg for understanding gcd. In fact, will use GCD is actually very simple, as long as remember two article can be.

1. The task of the home joins must be performed in an asynchronous function.

The task of the home joins will cause a deadlock if executed in the synchronization function. What is a deadlock? Let me say, for example.

See no, the task of the home team joins is not executed, the program is not executed down, this is called a deadlock. That's why there's a deadlock. The task of the home joins is executed by default in the main thread, and the program places the home joins task on the last side of the main thread. When the program executes to 2 o'clock, the task 1 will be placed behind the main thread, such as the main line thread tasks are executed in the execution. But what is this function? This is the synchronization function, it will block the current thread, the subsequent task will wait for task 1 to execute after execution. So they started to wait for each other and never went down.

Task 1 says to other tasks, "You're going to do it?" I can do it when you're done!

Other tasks on Task 1 say: No, you can do it when you're done. This is the synchronization of the two goods stipulated.

Task 1 and other tasks began a long wait ...

2. Tasks in the global concurrency queue execute in an asynchronous function to achieve concurrency.

We found that the program created three threads, each of which performed the task of downloading pictures, and three images started downloading almost at the same time. This achieves concurrency and does not clog the main thread. The most important point, after downloading the pictures, be sure to update in the main thread.

If interested can try to put the task in the concurrent queue in the synchronization function execution, we will find that three images are downloaded in the main thread, the image for a long time to download. Because the picture is one download, unlike the one that starts the download at the same time.

As for the custom queue, I will not talk about it, because it is similar to the above principle, and there are not many places. The concurrent queue system has been provided, so we don't have to create it ourselves. As for the serial queue, we use multithreading to do multitasking at the same time, the serial queue of tasks in the synchronous function or asynchronous function, it is impossible to do at the same time.

There are many other uses for GCD, and I'll talk about it in the next article.

Basic use of iOS multi-thread 5.GCD

Related Article

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.