Last active
September 25, 2015 08:58
-
-
Save edjames/896153 to your computer and use it in GitHub Desktop.
A quick example of multithreading
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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