From: Robert Klemme Date: 2010-02-16T00:47:27+09:00 Subject: Re: Mutex 2010/2/15 Mido Peace : > require 'thread' > class Semaphore > >  Thread.abort_on_exception = true Why do you place that inside the class? This is a global setting. >  attr_accessor :permits, :mutex, :cv > >  def initialize ( permits ) > >      @permits = permits >      @mutex = Mutex.new >      @cv = ConditionVariable.new > >  end > > >  def acquire() > >    raise "Interrupted Thread " if (!Thread.current.alive?) >    @mutex.synchronize { >    while @permits < 1 >       @cv.wait(@mutex) >    end >    @permits = @permits - 1 >    } >  end > >  def release() >    @mutex.synchronize{ > >      @permits += 1 >      @cv.signal > >      } > >  end > > end > > ... > class Process < Thread > ... > def initialize() >  super(){ >     ... >     @sem=Semaphore.new(0) IIRC semaphores are typically initialized with a positive value, i.e. something larger than 0. If you think about it, init with 0 does not make too much sense. >     ... > } > end > > def run() > > ... > @sem.acquire() > > end > > > The problem is that I should call the semaphore later , not within > initialization of the process !! You can't define a class Process like this because that class exists already. One more hint: for more robust access I suggest to add this method to your Semaphore class: def synchronize acquire begin yield ensure release end end Kind regards robert -- remember.guy do |as, often| as.you_can - without end http://blog.rubybestpractices.com/