Created
February 1, 2012 16:34
-
-
Save dpruessner/1717894 to your computer and use it in GitHub Desktop.
rbx multi-threaded test program
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
#!/usr/bin/env ruby | |
# | |
# Characterize rbx true multi-threading vs. mri1.9.2 | |
# | |
require 'digest/md5' | |
require 'thread' | |
######################################################## | |
# Number of threads in the program | |
THREADS = 5 | |
LEN_OUTER = 1000 | |
LEN_INNER = 400 | |
################# | |
Thread.abort_on_exception=true | |
STDOUT.sync=true | |
# Build out the dataset | |
# | |
ary = [] | |
count = 0 | |
mutex = Mutex.new | |
running = [] | |
ts = Time.now | |
thread_main = Thread.current | |
THREADS.times do | |
Thread.new do | |
mutex.synchronize{ running << Thread.current } | |
is_exit = false | |
loop do | |
# Build internal array, then add to master | |
inner_ary = [] | |
LEN_INNER.times{ inner_ary << rand() } | |
mutex.synchronize do | |
if count >= LEN_OUTER | |
is_exit = true | |
else | |
ary << inner_ary | |
count += 1 | |
end | |
end # synchronize | |
break if is_exit | |
end # loop | |
mutex.synchronize do | |
running.delete Thread.current | |
if running.length == 0 | |
thread_main.wakeup | |
end | |
end # synchronize | |
end # Thread | |
end # Number of Threads | |
sleep | |
puts "Generation: #{Time.now.-(ts).*(1e3).round(3)}ms" | |
puts "Length: #{ary.length}" | |
puts "DONE" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment