On Thu, Feb 27, 2003 at 12:15:37AM +0900, ahoward wrote: > On Wed, 26 Feb 2003, E F van de Laar wrote: > > <snip> > > I also get "Bad file descriptor" errors at times. > <snip> > i'm not sure, but accessing stdout/stderr from this many threads at once may > be a problem - i've had problems with it before. one thing i've done in my > threaded problems is something like this : > > #!/usr/bin/env ruby > require 'thread' > > tds = [] > toutput = [] > sem = Mutex.new > > 4.times do |tid| > toutput[tid] = [] > tds << > Thread.new (tid) do > 2.times do |i| > Thread.pass while not sem.try_lock > toutput[tid] << "msg #{i} from thread #{tid}..." > sem.unlock sem.synchronize do toutput[tid] << "msg #{i} from thread #{tid}..." end I feel Thread.* are sometimes too low-level. [snip] > this technique alone has helped my concurrent programming skiils since i can > read the bloody output! This makes me think of one doubt I've had for some time: what is the granularity of thread-switching? or What can I assume to happen atomically? Obviously calls to methods implemented in C and such must be atomic since Ruby's using green threads. However, I've found in my code things like this: (complete example at http://www.rubygarden.org/ruby?ObjectPoolingAndThreading) def run(debug = false) ref = [] handler = nil @mutex.synchronize do info = false if @pool.size == 0 @handleravail.wait @mutex info = true if debug end handler = @pool.shift puts "Got handler #{handler} after getting blocked." if info @threads[handler] = ref # hack cause want this # inside synchronize; perhaps not needed end ref << Thread.new do begin yield handler ensure @mutex.synchronize do @pool << handler @threads.delete handler @handleravail.signal end end end ref[0] end The kludge with 'ref' is to ensure that the state of @threads (which is a hash) is consistent. What I really want to do is @threads[handler] << Thread.new { } but if that section is not protected with a Mutex I don't feel safe. Probably being too cautious and thinking too much as if we had were native threads. But it might pay off if someday Ruby has real threading... -- _ _ | |__ __ _| |_ ___ _ __ ___ __ _ _ __ | '_ \ / _` | __/ __| '_ ` _ \ / _` | '_ \ | |_) | (_| | |_\__ \ | | | | | (_| | | | | |_.__/ \__,_|\__|___/_| |_| |_|\__,_|_| |_| Running Debian GNU/Linux Sid (unstable) batsman dot geo at yahoo dot com *** PUBLIC flooding detected from erikyyy <lewnie> THAT's an erik, pholx.... ;) -- Seen on #LinuxGER