Skip to content

Instantly share code, notes, and snippets.

@edjames
Last active September 25, 2015 08:58
Show Gist options
  • Save edjames/896153 to your computer and use it in GitHub Desktop.
Save edjames/896153 to your computer and use it in GitHub Desktop.
A quick example of multithreading
require 'benchmark'
class CottonThread
attr_reader :threads, :procs
def initialize
@threads, @procs = [], []
end
def sow(proc)
@procs << proc
end
def reap
@procs.each { |p| @threads << Thread.new{p.call} }
@threads.each { |t| t.join }
end
end
c = CottonThread.new
10.times { c.sow -> { 3.times { sleep 1 } } }
puts "Procs: #{c.procs.count}"
Benchmark.bm(7) do |bm|
bm.report { c.reap }
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment