creates threads as needed • Manages threads for concurrent queues based on processor utilization • Serial queues use overcommit queues • Can overwhelm system with blocks that pend for any reason
dispatch_get_global_queue( long priority, unsigned long flags); dispatch_queue_t dispatch_get_main_queue(void); • Create a private queue dispatch_queue_t dispatch_queue_create( const char *label dispatch_queue_attr_t attr); • All private queues target a global queue • dispatch_retain/dispatch_release
easily allows all work to be off main thread • Execute code in main thread (UI) dispatch_async(queue, ^{ // Do Something dispatch_async(dispatch_get_main_queue(), ^{ // Running on main thread. Do UI stuff here. }); });
}); // Small window for other stuff to be put on queue dispatch_async(queue, ^{ // Do Something Else }); dispatch_async(queue, ^{ // Do Something // Next block not put on queue until this one executes dispatch_async(queue, ^{ // Do Something Else }); });
before returning dispatch_sync(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{ // Do Something }); // Do not get here until the above block has run to completion • Can be useful as general synchronization primitive • Pair with async for read/write operations
(void)asyncHighPriority:(void(^)(void))block { dispatch_suspend(lowPriorityQueue); dispatch_async(highPriorityQueue, ^{ block(); dispatch_resume(lowPriorityQueue); }); } • What if did same without retargeting?
{ dispatch_barrier_async(queue_, block); } • Multiple threads executing read block • One thread executing write block • Make concurrent queue act like serial queue • Other way to make concurrent queue serial?
of abstraction (better?) • Fixed width concurrent queues • Implement easily with semaphores • Simulate with N serial queues (danger) • Operation dependencies