Skip to content

Instantly share code, notes, and snippets.

@drernie
Created February 9, 2010 01:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save drernie/298817 to your computer and use it in GitHub Desktop.
Save drernie/298817 to your computer and use it in GitHub Desktop.
require 'dispatch'
T=1
T0 = Time.now
def now
(Time.now - T0)
end
p "Call Dispatch.sync with delay #{T} @ #{now}"
Dispatch.sync {sleep T; p "Waited for me"}
p "Finished calling sync @ #{now}"
puts
p "Call Dispatch.async with delay #{T} @ #{now}"
Dispatch.async {sleep T; p "Didn't wait for me"}
p "Finished calling async @ #{now}"
sleep 2*T
puts
p "Calling Dispatch.fork with delay #{T} @ #{now}"
g = Dispatch.fork {sleep T; p "Tell me when done"}
p "Calling Dispatch.group with delay #{T} @ #{now}"
Dispatch.group(g) {sleep T; p "Me too"}
p "Calling Group.join to wait for both..."
g.wait
p "All done @ #{now}"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment