Dispatch::Queue Class

seanlilmateus edited this page Jan 13, 2013 · 6 revisions

GCD provides and manages dispatch queues to which your application can submit tasks in the form of block objects. You could easily create multi-threaded applications to use GCD.

GCD offers three kinds of queues:

  • Main queue: tasks execute serially on your application’s main thread
  • Concurrent queue: tasks are dequeued in FIFO order, but run concurrently and can finish in any order
  • Serial queue: tasks execute one at a time in FIFO order

Methods in Dispatch::Queue class


Creates a new serial queue object to which blocks can be submitted.

  • new(label) -> Queue
    • [PARAM] label:
      • Specify a string label to attach to the queue to uniquely identify.
    • [RETURN]
      • Returns a new Dispatch::Queue instance.


Retrieves a concurrent queue object to which blocks can be submitted.

  • concurrent(priority = :default) -> Queue
    • [PARAM] priority:
      • Specify the priority of queue. (:high, :low,:default)
    • [RETURN]
      • Returns a Dispatch::Queue instance.
gcdq = Dispatch::Queue.new('sample')
gcdq.after(Dispatch::TIME_NOW) {
  q = Dispatch::Queue.current
  puts q.label # => sample

sleep 1

q = Dispatch::Queue.current
puts q.label # => com.apple.main-thread


Retrieves a main queue object to which blocks can be submitted.

  • main -> Queue
    • [RETURN]
      • Returns a main queue instance.


Submits a block to a dispatch queue for multiple invocations. This method is declared in GCD API as dispatch_apply.

  • apply(iterations) {|index| ... }
    • [PARAM] iterations:
      • Specify a number of iterations to perform.
gcdq = Dispatch::Queue.new('sample')
@result = []
gcdq.apply(5) {|i| @result[i] = i*i }
p @result  #=> [0, 1, 4, 9, 16, 25]


Submits a block for asynchronous execution on a dispatch queue and returns immediately. This method is declared in GCD API as dispatch_group_async and dispatch_async. If specified the group argument, this method behaves as like dispatch_group_async.

  • async(group = nil) { ... }
    • [PARAM] group:
      • Specify a dispatch group to associate the submitted block object.
gcdq = Dispatch::Queue.new('sample')
@i = 0
gcdq.async { @i = 42 }
while @i == 0 do; end
p @i #=> 42
gcdq = Dispatch::Queue.new('sample')
gcdg = Dispatch::Group.new
@i = 0
gcdq.async(gcdg) { @i = 42 }
p @i #=> 42


Submits a block object for execution on a dispatch queue and waits until that block completes. This method is declared in GCD API as dispatch_sync.

  • sync { ... }
gcdq = Dispatch::Queue.new('sample')
@i = 0
gcdq.sync { @i = 42 }
p @i #=> 42


Enqueue a block for execution at the specified time. This method is declared in GCD API as dispatch_after.

  • after(delay) { ... }
    • [PARAM] delay:
      • Specify a time when submits a block to dispatch queue.
gcdq = Dispatch::Queue.new('sample')
gcdq.after(0.5) { puts "Hello" }

sleep 1


Returns the label specified for the queue when the queue was created. This method is declared in GCD API as dispatch_queue_get_label.

  • label -> String
    • [RETURN]
      • Returns the label of queue.
gcdq = Dispatch::Queue.new('sample')
gcdq.label # => 'sample'


This method is alias of Dispatch::Queue#label.






if you submit tasks to the Dispatch::Queue.main outside of an NSApplication or UIApplication. You need to call "Dispatch::Queue.main#run". Dispatch::Queue.main#run will act like a run loop and since it never returns, it will run forever.

Don't call Dispatch::Queue.main#run within an application, your submitted tasks will probably not be executed.

#!/usr/bin/env macruby -wKU

# Only running as a script
Dispatch::Queue.main.async { puts "ASYNC From THE MAIN" }
Dispatch::Queue.main.after(1) { puts "RUN AFTER From THE MAIN" }