OC Multi-thread GCD-----2

Source: Internet
Author: User

The queue generated by Dispatch_create is the default priority regardless of whether it is a parallel queue or a serial queue

But you can use Dispatch_set_target_queue to change the priority of the queue.

Dispatch_set_target_queue (original queue, target priority queue)

Use this function to obtain two queues, one for the queue that needs to change priority, and one for the priority queue (the queue with the specified priority can be obtained by Get_global)

If multiple serial queue priorities are the same, the tasks in these queues are also executed serially

The Dispatch_after function does not perform the task very precisely after a period of time, Dispatch_after only inserts the block into the queue after a specified time, and when the execution depends on the execution of the system

Delay how many triggers we all know, the following code is the use of nsdate to generate a time, let dispatch at this time to execute block, but this seemingly only accurate to points, not accurate to the second, it may be my code has a problem, the reason is also early search

@implementationnsdate (DIS)-(dispatch_time_t) getdis{nstimeinterval interval; DoubleSecond,subsecond; structTimespec time;            dispatch_time_t Milestone; Interval=[self timeIntervalSince1970]; Subsecond= MODF (interval, &second); Time.tv_sec=second; Time.tv_nsec= Subsecond *nsec_per_sec; Milestone= Dispatch_walltime (&time,0); returnMilestone;}@end

The sync function should be used with caution when using GCD, because a slight mistake will result in a deadlock

The Dispatch_apply function is the associated API for Asyn and group, which can execute a specified number of blocks but block threads (synchronous execution), as shown in the following code

     dispatch_apply (0), ^(size_t index) {        NSLog (@ "%zu  ", index);    });        NSLog (@ "done");

=============== Printing Results

2014-06-19 20:58:55.097 efvervq[5840:60b] 0

2014-06-19 20:58:55.097 efvervq[5840:1303] 1

2014-06-19 20:58:55.097 efvervq[5840:3803] 2

2014-06-19 20:58:55.099 efvervq[5840:60b] 4

2014-06-19 20:58:55.097 efvervq[5840:3903] 3

2014-06-19 20:58:55.099 efvervq[5840:1303] 5

2014-06-19 20:58:55.099 efvervq[5840:3803] 6

2014-06-19 20:58:55.099 efvervq[5840:60b] 7

2014-06-19 20:58:55.101 efvervq[5840:3803] 9

2014-06-19 20:58:55.099 efvervq[5840:3903] 8

2014-06-19 20:58:55.102 efvervq[5840:60b] Done



//=================

The order is different, but the done outside of the function is the last printed

PS. But don't mix it up. When you add blocks consecutively using the parallel queue and the sync function, the book sequence is 0123456789.

We can use the Apply function in the Async function to avoid blocking the host column

Dispatch_suspend (queue) can suspend the queues,

Dispatch_resume (queue) can restore the specified queue, and the system will continue to perform the unhandled operation without affecting the already executed

Sometimes very precise data security controls are required, so using Dispatch_semaphore can provide more granular access control than Serialqueue because this is controlled by semaphores

But the semaphore <0 is not allowed to be accessed at all times.

Look at the code below.

dispatch_queue_t queue = Dispatch_get_global_queue (Dispatch_queue_priority_default,0); /** * Generate Dispatch_semaphore * Set its count initial value to 1 * Ensure that the thread that accesses the Nsmutablearray class object can have only one*/dispatch_semaphore_t Semaphore= Dispatch_semaphore_create (1); Nsmutablearray*array =[Nsmutablearray array];  for(inti =0; I <10000; i++) {dispatch_async (queue,^{            /** * Wait Dispatch_semaphore * * Wait, know semaphore value is greater than or equal to 1*/dispatch_semaphore_wait (semaphore, dispatch_time_forever); /** * To do this, the semaphore is 1 and then the system automatically subtracts 1 and becomes 0 * at this time only the thread executing to this access array*/[Array addobject:[nsnumber numberwithint:i]; /** * After the end of the visit, give the semaphore +1. So that other threads continue to add objects like an array*/dispatch_semaphore_signal (semaphore);            }); }

GCD In addition to the queue there are dispatch_source, this feature can be used to deal with a very low resource consumption of file read and write, timers, signal reception and other events, more functions can be consulted Apple's documents

/** * Create a source dispatch_source_type_timer for the type of source (timer)*/dispatch_source_t Timer= Dispatch_source_create (Dispatch_source_type_timer,0,0, Dispatch_get_main_queue ()); /** * Set time * delay after 15 seconds * Repeat every 2 seconds * allow 1 seconds delay*/Dispatch_source_set_timer (Timer, Dispatch_time (Dispatch_time_now, 15ull* nsec_per_sec), 2ull * nsec_per_sec, 1ull *nsec_per_sec); //the tasks performed__blockintA =0; Dispatch_source_set_event_handler (Timer,^{NSLog (@"Wake up"); A++; if(A = =8) {dispatch_source_cancel (timer);        }    }); //Action on CancelDispatch_source_set_cancel_handler (Timer, ^{NSLog (@"cancled");            }); /** * If the MRC requires Dispatch_release*/            //Start WorkingDispatch_resume (timer); 

If you want to keep the timer from repeating, you can set the repetition time to Dispatch_timeforever or cancle after the execution of the Set_event_handler.

Set_event_handler inside must have cancel (or inside the method of execution has cancel, is can search), otherwise the timer will not execute, you can try it yourself

But you can cheat the system with if (0) {cancel}, but don't forget cancel

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.