W
wimxa
I made the following test:
#-------------------------------------------------------------------
require 'thread'
mutex = Mutex.new
c_produce = ConditionVariable.new
c_consume = ConditionVariable.new
full = false
t1 = Thread.new do
while true
mutex.synchronize do
c_produce.wait mutex while full
print 'F'
full = true
c_consume.signal
end
#sleep 0.001
end
end
t2 = Thread.new do
while true
mutex.synchronize do
c_consume.wait mutex while !full
print '.'
full = false
c_produce.signal
end
end
end
t1.join
t2.join
#-------------------------------------------------------------------
This should be a classic producer/consumer problem solution. The
output:
F.F.F.F.F.F.F.F.F.F.F.F.F.F.F.F.F.F.F.F.F.F.F.F.F.F.F.F.F.F.F.deadlock
0x2b68300: sleep:- - c:/ruby/projects/threads2.rb:9
deadlock 0x27bc74c: sleep:J(0x2b68300) (main) - c:/ruby/projects/
threads2.rb:27
deadlock 0x2b68274: sleep:- - c:/ruby/projects/threads2.rb:20
c:/ruby/projects/threads2.rb:20: Thread(0x2b68274): deadlock (fatal)
What is wrong with the code?
Also, uncommenting a sleep from above makes the deadlocks disappear
and everything works rather fine - is this just a matter of tweaking
the probability? It ran for quite a long time (several minutes),
that's why I doubt this, but hey - I just might be completely wrong.
Any ideas?
#-------------------------------------------------------------------
require 'thread'
mutex = Mutex.new
c_produce = ConditionVariable.new
c_consume = ConditionVariable.new
full = false
t1 = Thread.new do
while true
mutex.synchronize do
c_produce.wait mutex while full
print 'F'
full = true
c_consume.signal
end
#sleep 0.001
end
end
t2 = Thread.new do
while true
mutex.synchronize do
c_consume.wait mutex while !full
print '.'
full = false
c_produce.signal
end
end
end
t1.join
t2.join
#-------------------------------------------------------------------
This should be a classic producer/consumer problem solution. The
output:
F.F.F.F.F.F.F.F.F.F.F.F.F.F.F.F.F.F.F.F.F.F.F.F.F.F.F.F.F.F.F.deadlock
0x2b68300: sleep:- - c:/ruby/projects/threads2.rb:9
deadlock 0x27bc74c: sleep:J(0x2b68300) (main) - c:/ruby/projects/
threads2.rb:27
deadlock 0x2b68274: sleep:- - c:/ruby/projects/threads2.rb:20
c:/ruby/projects/threads2.rb:20: Thread(0x2b68274): deadlock (fatal)
What is wrong with the code?
ruby 1.8.6 (2007-03-13 patchlevel 0) [i386-mswin32]ruby -v
Also, uncommenting a sleep from above makes the deadlocks disappear
and everything works rather fine - is this just a matter of tweaking
the probability? It ran for quite a long time (several minutes),
that's why I doubt this, but hey - I just might be completely wrong.
Any ideas?