Skip to content
aleclarson edited this page Oct 21, 2014 · 7 revisions

Queue

Queues manage the execution of closures you give to them.

Serial queues execute their closures one at a time.

Concurrent queues execute their closures many at a time (depending on system conditions).

All queues always execute their closures in order. Serial queues' closures always finish in order. Concurrent queues' closures do not always finish in order.

myQueue.isConcurrent is useful for knowing what kind of queue you're working with.

Queue runs on top of the traditional dispatch queue.


Pre-existing Queues

gcd: The Dispatcher singleton. The concurrent queue for default-priority tasks.

gcd.main: The serial queue for the user interface. Learn more about the main queue.

gcd.high: The concurrent queue for high-priority tasks.

gcd.low: The concurrent queue for low-priority tasks.

gcd.background: The concurrent queue for zero-priority tasks.


Which Queue am I currently in?

gcd.main.async {
  let currentQueue: Queue = gcd.current // gcd.main
  let queueIsCurrent: Bool = currentQueue.isCurrent // true
}

current is only available on gcd.

isCurrent is available on all Queues.


How do I make my own Queues?

You must retain self-made queues!

let serialQueue = gcd.serial()
let concurrentQueue = gcd.concurrent()

Queue automatically prevents deadlocks!

Queues prevent deadlocks on the current queue. The example below would deadlock the main queue if you weren't using this framework!

gcd.main.sync {
  gcd.main.sync { // normally deadlocks
    println("hello world")
  }
}

Instead, the Queue just pretends like you did this:

gcd.main.sync {
  println("hello world")
}
Clone this wiki locally