def test_multi_access
conn_count = 0
conn_lock = Mutex.new
conn_cond = ConditionVariable.new
conn_wait = proc{|nth|
conn_lock.synchronize{
until (conn_count == nth)
conn_cond.wait(conn_lock)
end
}
}
conn_next = proc{
sleep(0.1)
conn_lock.synchronize{
conn_count += 1
conn_cond.broadcast
}
}
start_flag = false
start_lock = Mutex.new
start_cond = ConditionVariable.new
start_wait = proc{
start_lock.synchronize{
until (start_flag)
start_cond.wait(start_lock)
end
}
}
start_go = proc{
start_lock.synchronize{
start_flag = true
start_cond.broadcast
}
}
th_grp = ThreadGroup.new
nconns = 64
nreqs = 32
nconns.times do |i|
th_grp.add(make_access_thread(i, nreqs, conn_wait, conn_next, start_wait))
end
conn_wait.call(nconns)
start_go.call
for th in th_grp.list
th.join
end
end