3
class ThreadScalability
6
@cond_var = ConditionVariable.new
11
value = "0123456789" * 10000
12
@array_size.times {|count|
13
@array[count] = value.clone
28
concurrency = @num_threads
29
num_ops_per_thread = @array_size/@num_threads
31
puts "concurrency is - #{concurrency}"
32
concurrency.times { |i|
33
threads << Thread.new {
34
puts "started thread #{i}"
36
# make sure all the threads start at the same time.
38
@num_ready_threads += 1
39
@cond_var.wait(@mutex) while @num_ready_threads < @num_threads
40
@cond_var.signal if @num_ready_threads == @num_threads
43
puts "Thread #{i} running"
46
reverse_strings(i*num_ops_per_thread, (i+1)*num_ops_per_thread - 1)
48
puts "Thread #{i} done"
56
#to reverse a range of String elements in @array.
57
def reverse_strings(beginIndex, endIndex)
58
while beginIndex <= endIndex
59
puts "another 10 in #{Thread.current.inspect}" if beginIndex % 10 == 0
60
reverse_string(@array[beginIndex])
71
puts "Time: #{Time.now - t}"
77
ThreadScalability.new.run(ARGV[0].to_i)