2010/2/15 Mido Peace <mido.peace / gmail.com>:
> require 'thread'
> class Semaphore
>
> =A0Thread.abort_on_exception =3D true

Why do you place that inside the class?  This is a global setting.

> =A0attr_accessor :permits, :mutex, :cv
>
> =A0def initialize ( permits )
>
> =A0 =A0 =A0@permits =3D permits
> =A0 =A0 =A0@mutex =3D Mutex.new
> =A0 =A0 =A0@cv =3D ConditionVariable.new
>
> =A0end
>
>
> =A0def acquire()
>
> =A0 =A0raise "Interrupted Thread " if (!Thread.current.alive?)
> =A0 =A0 / mutex.synchronize {
> =A0 =A0while @permits < 1
> =A0 =A0 =A0 @cv.wait(@mutex)
> =A0 =A0end
> =A0 =A0@permits =3D @permits - 1
> =A0 =A0}
> =A0end
>
> =A0def release()
> =A0 =A0 / mutex.synchronize{
>
> =A0 =A0 =A0@permits +=3D 1
> =A0 =A0 =A0 / cv.signal
>
> =A0 =A0 =A0}
>
> =A0end
>
> end
>
> ...
> class Process < Thread
> ...
> def initialize()
> =A0super(){
> =A0 =A0 ...
> =A0 =A0 @sem=3DSemaphore.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.

> =A0 =A0 ...
> }
> 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 alread=
y.

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

--=20
remember.guy do |as, often| as.you_can - without end
http://blog.rubybestpractices.com/