1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78
|
require 'thread'
class ThreadScalability
def initialize()
@mutex = Mutex.new
@cond_var = ConditionVariable.new
@num_ready_threads = 0
@num_threads = 0
@array_size = 120
@array = []
value = "0123456789" * 10000
@array_size.times {|count|
@array[count] = value.clone
}
end
def reverse_string(s)
len = s.length
(len/2).times { |i|
tmp = s[i]
s[i] = s[len -1 - i]
s[len -1 -i] = tmp
}
return s
end
def do_test()
concurrency = @num_threads
num_ops_per_thread = @array_size/@num_threads
threads = []
puts "concurrency is - #{concurrency}"
concurrency.times { |i|
threads << Thread.new {
puts "started thread #{i}"
# make sure all the threads start at the same time.
@mutex.synchronize {
@num_ready_threads += 1
@cond_var.wait(@mutex) while @num_ready_threads < @num_threads
@cond_var.signal if @num_ready_threads == @num_threads
}
puts "Thread #{i} running"
#now do the work.
reverse_strings(i*num_ops_per_thread, (i+1)*num_ops_per_thread - 1)
puts "Thread #{i} done"
}
}
threads.each {|t|
t.join
}
end
#to reverse a range of String elements in @array.
def reverse_strings(beginIndex, endIndex)
while beginIndex <= endIndex
puts "another 10 in #{Thread.current.inspect}" if beginIndex % 10 == 0
reverse_string(@array[beginIndex])
beginIndex += 1
end
end
def run(argument)
count = argument.to_i
@num_threads = count
t = Time.now
do_test()
puts "Time: #{Time.now - t}"
end
end
5.times {
ThreadScalability.new.run(ARGV[0].to_i)
}
|