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:
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?
it seems like you are making if very hard on yourself. why not
something like this:
cfp:~ > cat a.rb
require ‘thread’
q = SizedQueue.new 42
producer = Thread.new do
loop do
q.push Time.now.to_f
end
end
consumer = Thread.new do
loop do
p(( data = q.pop ))
end
end
producer.join
consumer.join
??
if you really want to roll your own stuff check out the code in
thread.rb - the Queue and SizedQueue classes are basically the code
you are writing above.
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 think you at least need a newer patchlevel of 1.8.6; IIRC, there are
bugs in the mutex implementation that shipped with 1.8.6p0.
it seems like you are making if very hard on yourself.
No, this is just a test. I have another problem requiring the
synchronization. It is not working, so I made this test to see what is
I am doing wrong. It seems that it is not a problem with the other
code, but the part of the code that is similar to what I have made in
the above test. However, thanks for the pointers - I will take a look
at them and I might just find an answer.
Do you need some more info (and how can I obtain it)?
Also, is there some good documentation about multi-threading in ruby,
especially the synchronization part? I have looked at some tutorials
(e.g. Programming Ruby: The Pragmatic Programmer’s Guide), but they
are sparse on this topic (just a few examples, not even near to what I
need). Any others?