About main Dispatch queue and global Dispatch queue.
The previous article said the way to create a Dispatch queue, in fact, without the dispatch_queue_create function can also get Dispatch queue, which is the main Dispatch queue and Global Dispatch queue.
As the name implies, the main Dispatch queue is the Dispatch queue executed in the main thread, the main thread can only have one, so the main Dispatch queue is a serial Dispatch queue, which is serial Dispatch Queue.
All processing that is appended to the main Dispatch queue is performed in Runloop. Because it is executed in the main thread, it is used in the main Dispatch queue to append some processing that must be performed in the main thread, such as updates to the user interface. This is similar to the Performselectoronmainthread instance method of the NSObject class.
The Global Dispatch queue is the concurrent Dispatch queue. It is not necessary to generate the concurrent Dispatch queue one by one with the Dispatch_queue_create function, as long as the global Dispatch queue is available. The Global Dispatch queue has four execution priorities, namely high priority, the default priority, the lower priority, and the background priority (Background Priority).
Here's how to get the main Dispatch queue:
/* * * */-(void) getmaindispatchqueue {= dispatch_get_main_queue () ; ^{ NSLog (@ "%@: Mainqueue is main thread:%i", [nsthread CurrentThread], [Nsthread ismainthread]); });
Output Result:
-£º:48.710 gcd_study[19270:607 ] {name = (null1}: Mainqueue is main thread:1
The Dispatch_get_main_queue function is the way to get the main dispatch queue. Also, remember to append the task to the main Dispatch queue with the Dispatch_async function, because the Dispatch_sync function will not return the task until the specified task is executed, if it is appended to it, so if the Dispatch_ The sync function appends the task to the main Dispatch queue, causing the program to deadlock.
The Dispatch_get_main_queue function is defined as follows:
/* ! * @function dispatch_get_main_queue * * @abstract * Returns The default queue, which is bound to the main thread. * * @discussion * In order to invoke blocks submitted to the main queue, the application must * call Dispatch_main (), NSAp Plicationmain (), or use a cfrunloop on the main * thread. * * @result * Returns the main queue. This queue was created automatically on behalf of * the main thread before main () is called. */ __osx_available_starting (__mac_10_ 6,__IPHONE_4_0) dispatch_export Dispatch_queue_s _dispatch_main_q; #define dispatch_get_main_queue ()
Here's how to get the global Dispatch queue:
/** * Get four priority global Dispatch Queue*/- (void) Getglobaldispatchqueue {//High-priority Dispatch_queue_priority_highdispatch_queue_t highpriorityqueue = Dispatch_get_global_queue (Dispatch_queue_priority_high,0); //Default Priority Dispatch_queue_priority_defaultdispatch_queue_t defaultpriorityqueue = Dispatch_get_global_queue (Dispatch_queue_priority_default,0); //low-priority Dispatch_queue_priority_lowdispatch_queue_t lowpriorityqueue = Dispatch_get_global_queue (Dispatch_queue_priority_low,0); //Background Priority Dispatch_queue_priority_backgrounddispatch_queue_t backgroundpriorityqueue = Dispatch_get_global_queue (Dispatch_queue_priority_background,0);}
Dispatch_get_global_queue need to pass two parameters, the first one is to specify the priority to get, the second is a flogs value, the source code is defined as:
/*! * @function dispatch_get_global_queue * * @abstract * Returns A well-known global concurrent queue of a given priori Ty level. * * @discussion * The well-known global concurrent queues May is modified. Calls to * dispatch_suspend (), Dispatch_resume (), Dispatch_set_context (), etc., would * has no effect when used with queue S returned by the This function. * * @param priority * A defined in dispatch_queue_priority_t * * @param the flags * Reserved for the future use. Passing any value and other than zero if result in * a NULL return value. * * @result * Returns the requested global queue. */__osx_available_starting (__mac_10_6,__iphone_4_0) dispatch_export dispatch_const DISPATCH_WARN_RESULT DISPATCH _nothrowdispatch_queue_tdispatch_get_global_queue (dispatch_queue_priority_t priority, unsignedLongFlags);
The flags are reserved for later use, now only 0, and any value greater than 0 can only return a null.
Also, there is no problem with executing dispatch_retain or dispatch_release for the main Dispatch queue and global Dispatch queue.
Use of CD Multi-Threading (iii)