require 'thread'
class Semaphore

  Thread.abort_on_exception = true
  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)
     ...
}
end

def run()

...
@sem.acquire()

end


The problem is that I should call the semaphore later , not within 
initialization of the process !!





-- 
Posted via http://www.ruby-forum.com/.