+class MySemaphore
+ Thread.abort_on_exception = true
+ attr_accessor :permits
+
+
+ def initialize (permits = 0)
+ @permits = permits
+ end
+
+ def acquire(mutex,cv)
+
+ raise "Interrupted Thread " if (!Thread.current.alive?)
+ mutex.synchronize {
+ while @permits <= 0
+
+ cv.wait(mutex)
+
+ end
+ @permits = @permits - 1
+ cv.signal
+ }
+
+ end
+
+ def release(mutex,cv)
+ mutex.synchronize{
+ @permits += 1
+ cv.signal
+ }
+ end
+end
+#######################################
+# Another Semaphore
+#######################################