Concurrency
suggest changeIntroduction
Related topic: Grand Central Dispatch
Syntax
dispatch_asyncschedules a block on a queue and returns immediately. Code after the call keeps running. The block may run at the same time, on another thread, if that queue is not the caller’s queue.dispatch_syncschedules a block on a queue and blocks the calling thread until the block finishes. Callingdispatch_syncon the same queue that is already executing the caller deadlocks.
Parameters
| Parameter | Details |
|---|---|
queue |
The queue the block runs on. A queue is not a thread: GCD runs queue blocks on a pool of threads. dispatch_get_main_queue() is the main queue. Create a queue with dispatch_queue_create("QUEUE_NAME", DISPATCH_QUEUE_CONCURRENT) or DISPATCH_QUEUE_SERIAL. The name shows up in the debugger. DISPATCH_QUEUE_CONCURRENT may run blocks in parallel. DISPATCH_QUEUE_SERIAL runs one block at a time. |
block |
Code that runs on queue. In Xcode, double-click a block argument outlined in blue to insert an empty block. |
Remarks
Work on a queue other than the main queue must stay thread-safe. Many UIView methods crash or do nothing off the main thread. Do not change variables or properties that the main thread also uses, unless that access is synchronized.
Found a mistake? Have a question or improvement idea?
Let me know.
Table Of Contents