Methods for controlling thread execution through dispatch queues in IOS applications _ios

Source: Internet
Author: User
Tags closure gcd

The core of GCD programming is the dispatch queue, where the execution of the dispatch block is eventually put into a queue, similar to Nsoperationqueue but more complex and powerful, and can be nested. So, combining the GCD of block implementation, the feature of function closure (Closure) is fully played.

There are several ways in which dispatch queues can be built:

1. dispatch_queue_t queue = dispatch_queue_create ("com.dispatch.serial", dispatch_queue_serial); Generates a serial queue in which the blocks in the queue are executed in the order of FIFO, and are actually single-threaded. The first parameter is the name of the queue, which is useful when debugging the program, all as far as possible.

2. dispatch_queue_t queue = dispatch_queue_create ("Com.dispatch.concurrent", dispatch_queue_concurrent); Generates a concurrent execution queue, which is distributed to multiple threads to execute

3. dispatch_queue_t queue = Dispatch_get_global_queue (Dispatch_queue_priority_default, 0); Get the concurrent queues that the program processes default to, and prioritize to select High, Medium, and low three priority queues. Because the system is generated by default, Dispatch_resume () and dispatch_suspend () cannot be invoked to control execution continuation or interruption. It should be noted that three queues do not represent three threads, and there may be more threads. Concurrent queues can automatically generate a reasonable number of threads according to the actual situation, or it can be understood that the dispatch queue implements a thread pool management, which is transparent to the program logic.

The official website document explains that there are three concurrent queues, but there is actually a lower-priority queue, setting priority to Dispatch_queue_priority_background. Xcode you can observe the various dispatch queues that are in use when you debug.

4. dispatch_queue_t queue = Dispatch_get_main_queue (); Gets the dispatch queue of the main thread, which is actually a serial queue. It is also not possible to control execution continuation or interruption of the main thread dispatch queue.

Threading Operations Sample
to facilitate the use of GCD, Apple offers a number of ways for us to put the block on either the main thread or the background threads, or postpone execution. The examples used are as follows:

Copy Code code as follows:

Background execution:
Dispatch_async (dispatch_get_global_queue (0, 0), ^{
Something
});

Copy Code code as follows:

Main thread Execution:
Dispatch_async (Dispatch_get_main_queue (), ^{
Something
});

Copy Code code as follows:

One-time execution:
Static dispatch_once_t Oncetoken;
Dispatch_once (&oncetoken, ^{
Code to is executed once
});

Copy Code code as follows:

Delay 2 seconds for execution:
Double delayinseconds = 2.0;
dispatch_time_t poptime = Dispatch_time (Dispatch_time_now, delayinseconds * nsec_per_sec);
Dispatch_after (Poptime, Dispatch_get_main_queue (), ^ (void) {
Code to is executed on the main queue after delay
});

Dispatch_queue_t can also be defined yourself, such as to customize queue, you can use the Dispatch_queue_create method, examples are as follows:
Copy Code code as follows:

Custom dispatch_queue_t
dispatch_queue_t urls_queue = dispatch_queue_create ("blog.devtang.com", NULL);
Dispatch_async (Urls_queue, ^{
Your code
});
Dispatch_release (Urls_queue);

In addition, GCD has some advanced usage, such as having two threads running in parallel in the background, and then summarizing the execution results when all two threads are finished. This can be implemented with dispatch_group_t, Dispatch_group_async, and Dispatch_group_notify, as shown in the following example:
Copy Code code as follows:

Merge Rollup Results
dispatch_group_t group = Dispatch_group_create ();
Dispatch_group_async (Group, Dispatch_get_global_queue (0,0), ^{
Line Cheng for parallel execution
});
Dispatch_group_async (Group, Dispatch_get_global_queue (0,0), ^{
Two threads executing in parallel
});
Dispatch_group_notify (Group, Dispatch_get_global_queue (0,0), ^{
Summary results
});

The dispatch queue does not support Cancel (cancel), does not implement the Dispatch_cancel () function, unlike Nsoperationqueue, I have to say this is a small flaw.

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.