A preliminary study of iOS multi-threading (eight)--dispatch queue

Source: Internet
Author: User
Tags gcd
<span id="Label3"></p><p><p>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 more powerful, and can be Nested. so, combined with block implementation of the gcd, the function closure (Closure) of the characteristics of the full play. There are several ways in which the dispatch queue can be generated:</p></p><p><p>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 first in, out (FIFO) order, which is actually a single thread. The first parameter is the name of the queue, which is useful when debugging a program, with all the same names as Possible.</p></p><p><p>2. dispatch_queue_t queue = dispatch_queue_create ("com.dispatch.concurrent", dispatch_queue_concurrent); Generates a concurrent execution queue, and the block is distributed to multiple threads to execute</p></p><p><p>3. dispatch_queue_t queue = Dispatch_get_global_queue (dispatch_queue_priority_default, 0); Gets the concurrent queue generated by the program process by default, which can be prioritized to select high, medium, and low three priority queues. Because the system is generated by default, Dispatch_resume () and Dispatch_suspend () cannot be called to control execution continuation or Interruption.</p></p><p><p>It is important to note that three queues do not represent three threads and there may be more threads. Concurrent queues can automatically generate a reasonable number of threads based on the actual situation, or they can be understood as a thread pool managed by the dispatch queue and transparent to the program Logic.</p></p><p><p>The official website document explains that there are three concurrent queues, but there is actually a lower priority queue with a priority of dispatch_queue_priority_background. The individual dispatch queues that are being used can be observed during Xcode debugging.</p></p><p><p>4. dispatch_queue_t queue = Dispatch_get_main_queue (); Get the dispatch queue for the main thread, which is actually a serial Queue. It is also impossible to control the continuation or interruption of the main thread dispatch queue Execution. Next we can use the Dispatch_async or Dispatch_sync function to load the block that needs to be Run.</p></p><p><p>Dispatch_async (queue, ^{//block Specific code}); Execute block asynchronously, function returns immediately</p></p><p><p>Dispatch_sync (queue, ^{//block Specific code}); Execute block synchronously, function does not return, wait until block Executes. The compiler will optimize the code according to the actual situation, so sometimes you will find that the block is actually executing on the current thread and does not create a new thread. The actual programming experience tells us to avoid using dispatch_sync as much as possible, and it is easy to cause the deadlock of the program when nested in Use. If Queue1 is a serial queue, this code immediately generates a Deadlock:</p></p><p><p>Dispatch_sync (queue1, ^{</p></p><p><p>Dispatch_sync (queue1, ^{...});</p></p><p><p>......</p></p><p><p>});</p></p><p><p>Consider why the following code performs a deadlock in the main thread:</p></p><p><p>Dispatch_sync (dispatch_get_main_queue (), ^{...});</p></p><p><p>In practice, it is generally possible to write using dispatch, a common multithreaded execution model for network request Data: Dispatch_async (dispatch_get_global_queue (dispatch_queue_priority_ DEFAULT, 0), ^{</p></p><p><p>Start Network request data in a child thread</p></p><p><p>Updating the data Model</p></p><p><p>Dispatch_sync (dispatch_get_main_queue (), ^{</p></p><p><p>Updating UI code in the main thread</p></p><p><p>});</p></p><p><p>}); The Program's background runs and UI update code is compact and the code logic is at a glance.</p></p><p><p>The dispatch queue is thread-safe and can be used to implement a lock function with a serial Queue. For example, multi-threaded write the same database, need to keep the write order and the integrity of each write, simply use the serial queue to Achieve:</p></p><p><p>dispatch_queue_t queue1 = dispatch_queue_create ("com.dispatch.writedb", dispatch_queue_serial);</p></p><p><p>-(void) writedb: (nsdata *) Data {</p></p><p><p>Dispatch_async (queue1, ^{</p></p><p><p>Write database</p></p><p><p>});</p></p><p><p>}</p></p><p><p>Next call to writedb: you must wait until the last call is complete before you make the Writedb: the method is Thread-safe.</p></p><p><p>The dispatch queue also implements some other common functions, including:</p></p><p><p>void dispatch_apply (size_t iterations, dispatch_queue_t queue, void (^block) (size_t)); To repeat the block, it is important to note that this method is returned in a synchronous manner, that is, until all blocks have been executed before returning, and if an asynchronous return is required, it is nested in dispatch_async. Whether multiple blocks run concurrently or serially is also dependent on whether the queue is concurrent or serial.</p></p><p><p>void Dispatch_barrier_async (dispatch_queue_t queue, dispatch_block_t block); This function can set the block to be executed synchronously, and it will wait until the block before it joins the queue is completed before execution begins. After it joins the block of the queue, it waits until the block has been executed before it Executes.</p></p><p><p>void Dispatch_barrier_sync (dispatch_queue_t queue, dispatch_block_t block); ditto, except that it is a synchronous return function</p></p><p><p>void Dispatch_after (dispatch_time_t when, dispatch_queue_t queue, dispatch_block_t block); Deferred execution block finally, Let's look at a very special function of the dispatch queue<span style="font-size: 12.5px;"> </span> :</p></p><p><p><span style="font-size: 12.5px;">void Dispatch_set_target_queue (dispatch_object_t object, dispatch_queue_t queue); It assigns the task object that needs to be executed to a different queue for processing, the task object can be a dispatch queue, or it can be a dispatch source (described later Boven). And this process can be dynamic, can realize the dynamic scheduling of the queue management and so On. For example, there are two queues Dispatcha and dispatchb, when Dispatcha is assigned to Dispatchb:</span></p></p><p><p><span style="font-size: 12.5px;">Dispatch_set_target_queue (dispatcha, dispatchb);</span></p></p><p><p><span style="font-size: 12.5px;">Blocks that are not yet running on the Dispatcha will run on Dispatchb.</span></p></p><p><p><span style="font-size: 12.5px;">At this point, if you pause Dispatcha run:</span></p></p><p><p><span style="font-size: 12.5px;">Dispatch_suspend (dispatcha);</span></p></p><p><p><span style="font-size: 12.5px;">Will only halt the execution of the original block on the dispatcha, and the DISPATCHB block is Unaffected. If the DISPATCHB is paused, the run of Dispatcha is Paused. Here are just a few examples of how flexible the dispatch queue can be, and in practice you will gradually explore its potential. The dispatch queue does not support cancel (cancel) and does not implement the Dispatch_cancel () function, unlike nsoperationqueue, which has to be said to be a minor disappointment.</span><br></p></p><p><p> iOS multi-threading preliminary study (eight)--dispatch queue </p> </p></span>
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.