GCD introduction (4): Conclusion

Source: Internet
Author: User

Dispatch queue suspended

Dispatch queue can be suspended and restored. UseDispatch_suspendFunction to suspend, useDispatch_resumeFunction. The actions of these two functions are as expected. In addition, these two can also be used for dispatch source.

Note that the suspension of dispatch queue is block granularity. In other words, suspending a queue does not suspend the block currently being executed. It allows the currently executed block to be executed, and the subsequent block will not be executed until the queue is restored.

Another note: From the man page: If you suspend a queue or source, you must restore it before destroying it.

Specify the target of the dispatch queue

All user queues have a target queue concept. Essentially, a user queue does not actually execute any tasks, but it passes the tasks to its target queue for execution. Generally, the target queue is a global queue with the default priority.

Functions can be used for the target queue of a user queue.Dispatch_set_target_queue. We can pass any dispatch queue to this function, or even another user queue, as long as it does not constitute a loop. This function can be used to set the priority of a user queue. For example, if we can set the target queue of the user queue as a global queue with a low priority, all tasks in our user queue will be executed with a low priority. The same applies to high priorities.

One purpose is to set the user queue as main queue. This causes all blocks submitted to the user queue to be executed in the main thread. In this way, it is executed directly in the main thread.CodeThe advantage is that our user queue can be independently suspended and restored, and can also be reset to a global queue, then all the blocks will be executed on the global Queue (as long as you make sure your code leaves the main thread, there will be no problem ).

Another purpose is to specify the target queue of one user queue as another user queue. In this way, multiple queues can be forced to execute sequentially. This is enough to build a group of queues. By suspending and temporarily stopping the target queue, We can suspend and pause the entire group. ImagineProgram: It scans a group of directories and loads contents in the directories. To avoid disk competition, make sure that only one file loading task is executed on the same physical disk. You want to read multiple files from different physical disks at the same time. To achieve this, we need to create a dispatch queue structure, which is an image of the disk structure.

First, we will scan the system and find each disk to create a user queue for each disk. Scan the file system, create a user queue for each file system, and direct the target queue columns of these user queues to the appropriate disk user queue. Finally, each directory scanner has its own queue, and its target queue points to the queue of the file system where the directory is located. The directory scanner enumerates its own directories and submits a block for each file to its queue. Because the entire system is createdCubeIn this way, each physical disk is accessed in serial mode, and multiple physical disks are accessed in parallel. In addition to the queue initialization process, we do not need to do anything manually.

Semaphores

The semaphores of dispatch are the same as those of other semaphores. If you are familiar with semaphores in other multi-threaded systems, you may not be able to understand this section.

Semaphores are an integer value with an initial count value and support two operations: Signal notification and waiting. When a semaphore is notified, its count is increased. When a thread waits for a semaphore, the thread is blocked (if necessary) until the counter is greater than zero, and then the thread reduces the count.

We use functionsDispatch_semaphore_createTo create a dispatch semaphore. Use the FunctionDispatch_semaphore_signalTo receive notifications, use the FunctionDispatch_semaphore_waitTo wait. The man pages of these functions have two good examples, showing how to use semaphores to synchronize tasks and limited resource access control.

Single Initialization

GCD also provides support for word initialization, which is similar to the function in pthread.Pthread_onceVery similar. The advantage of GCD is that it uses block instead of function pointers, which allows a more natural way of code:

This feature is mainly used for inert Singleton initialization or other thread-safe data sharing. The typical Singleton Initialization Technology looks like this (thread-safe ):

 
+ (ID) sharedwhatever {static whatever * whatever = nil; @ synchronized ([whatever class]) {If (! Whatever) Whatever = [[whatever alloc] init];} return whatever ;}

This is good, but it is expensive.+ SharedwhateverThe function will pay for the lock, even if the lock only needs to be performed once. There is indeed a more cool way to achieve this, using something similar to a pair of locks or atomic operations, but this is quite difficult and error-prone.

Using GCD, we can rewrite the above method and use the FunctionDispatch_once:

 
+ (ID) sharedwhatever {static dispatch_once_t Pred; static whatever * whatever = nil; dispatch_once (& Pred, ^ {whatever = [whatever alloc] init];}); return whatever ;}

This is slightly better@ SynchronizedThe method is simpler, and GCD ensures that the detection is completed in a faster way. It ensures that the code in the block passes through any thread.Dispatch_onceIt is executed before the call, but it does not force the code to perform synchronous control every time this function is called. In fact, if you look at the header file where the function is located, you will find that its current implementation is actually a macro and has carried out an inline initialization test, which means that normally, you don't have to pay for the load of function calls, and there will be less synchronization control load.

Conclusion

This chapter describes the suspension, restoration, and target resetting of the dispatch queue, as well as the usage of these functions. In addition, we also introduced how to use the dispatch semaphore and single-time initialization functions. At this point, I have completed how GCD works and how to use it.

 

original article transferred from: http://www.dreamingwish.com/dream-2012/gcd-four-the-the-odds-and-ends.html

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.