From: Roman Faceless Date: 2011-11-14T05:35:11+09:00 Subject: Re: model "lift-passengers" and concurrent programming I took it from this site: # # $Id: semaphore.rb,v 1.2 2003/03/15 20:10:10 fukumoto Exp $ # class CountingSemaphore def initialize(initvalue = 0) @counter = initvalue @waiting_list = [] end def wait Thread.critical = true if (@counter -= 1) < 0 @waiting_list.push(Thread.current) Thread.stop end self ensure Thread.critical = false end def signal Thread.critical = true begin if (@counter += 1) <= 0 t = @waiting_list.shift t.wakeup if t end rescue ThreadError retry end self ensure Thread.critical = false end alias down wait alias up signal alias P wait alias V signal def exclusive wait yield ensure signal end alias synchronize exclusive end Semaphore = CountingSemaphore I took the implementation from this site http://www.imasy.or.jp/~fukumoto/ruby/ -- Posted via http://www.ruby-forum.com/.