Skip to content

Instantly share code, notes, and snippets.

@dpruessner
Created February 1, 2012 16:34
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 dpruessner/1717894 to your computer and use it in GitHub Desktop.
Save dpruessner/1717894 to your computer and use it in GitHub Desktop.
rbx multi-threaded test program
#!/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