From: ara.t.howard@... Date: 2006-07-10T00:15:04+09:00 Subject: Re: Deadlockproblem On Sun, 9 Jul 2006, Asterix Gallier wrote: > Hello, > > i'm trying to realize asynchronous communication, but the sender have to > wait until the answer is recieved. but that's synchronous! ;-) seriously, if the send always waits for the answer what good does it do thread your program? > My questions: > - why did this deadlock occur, and how can it be fixed among other things, you never do anything with @waitQueue - you simply always set @waitLock to true. > - is there another/better way for reaching this goal harp:~ > cat a.rb require 'thread' class Oracle def initialize @questions = Queue.new @thread = Thread.new(@questions){|q| loop{ question, answer_with = q.pop answer_with[question.to_s + ' 42'] } } end def ask question, &answer_with if answer_with @questions.push [question, answer_with] else q = Queue.new ask(question){|answer| q.push answer} Thread.new{ q.pop } end end def self.instance(*a, &b) @instance ||= new(*a, &b) end def self.ask(*a, &b) instance.ask *a, &b end end # # you can block up front # n = -1 p Oracle.ask("something #{ n += 1 }:").value # # or ask and reap later # threads = [] 7.times{ threads << Oracle.ask("something #{ n += 1 }:") } threads.map{|t| p t.value} harp:~ > ruby a.rb "something 0: 42" "something 1: 42" "something 2: 42" "something 3: 42" "something 4: 42" "something 5: 42" "something 6: 42" "something 7: 42" regards. -a -- suffering increases your inner strength. also, the wishing for suffering makes the suffering disappear. - h.h. the 14th dali lama