Target designation of GCD

Source: Internet
Author: User

dispatch_set_target_queue这个函数想找具体的代码来实现一下,找了半天没找到怎么来试验它,在51CTO上找了篇博文,看着还不错。

All user queues have a target queue concept. Essentially, a user queue does not actually perform any tasks, but it passes the task to its target queue for execution. Typically, the destination queue is the default priority of the global queue.

The target queue of the user queue can be modified with a function dispatch_set_target_queue . We can pass any dispatch queue to this function, or even another user queue, as long as you don't make a loop. This function can be used to set the priority of the user queue. For example, if we can set the target queue of the user queue to a low priority global queue, then the tasks in our user queue will be executed at a low priority level. High priority is the same thing.

One use is to set the target of the user queue as the main queue. This causes all blocks submitted to the user queue to be executed in the main thread. The advantage of doing this instead of executing code directly in the main thread is that our user queue can be suspended and restored individually, and the target can be reset to a global queue, and then all blocks become executed on the global queue (as long as you ensure that your code leaves the main thread without problems).

Another use is to designate the target queue for one user queue as a second user queue. This forces multiple queues to be executed serially in a coordinated manner, which is enough to build a set of queues that we can suspend and pause the entire group by suspending and pausing that target queue. Imagine a program that scans a set of directories and loads the contents of a directory. To avoid disk contention, we want to make sure that only one file load task is executing on the same physical disk. Instead, you want to be able to read multiple files from different physical disks at the same time. To achieve this, all we have to do is create a dispatch queue structure that mirrors the disk structure.

First, we scan the system and find each disk, creating a user queue for each disk. Then scan the file system and create a user queue for each file system to point the target queue of those user queues to the appropriate disk user queue. Finally, each directory scanner has its own queue, and its destination queue points to the queue of the file system where the directory resides. The directory scanner enumerates its own directories and submits a block to its own queue for each file. Because of the way the whole system is built, each physical disk is accessed serially, and multiple physical disks are accessed in parallel. In addition to the queue initialization process, we do not need to manually intervene in anything at all.

Reference: http://mobile.51cto.com/hot-403005.htm

Target designation of 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.