Here are several approaches for synchronization around a pool of limited resources that are checked in and out by a gang of threads.
There are three different pools: MonitorPool
which uses monitor.rb
, MutexCondvarPool
which uses a Mutex
and a ConditionVariable
, and SemaphorePool
which uses a java.util.concurrent.Semaphore
(only under JRuby).
The Request
class simply runs in a thread, tries to acquire a resource from the pool, keeps trying until it has held it at least once, sleeps for a busy-wait moment, and then checks it back into the pool.
$ ruby pool.rb [M] [N] [TIMEOUT] [BUSY]
where M is the number of resources, N is the number of threads contending for the resources, TIMEOUT is the duration (in seconds) a thread will wait for a resource before retrying, and BUSY is the duration (in seconds) a thread will spend holding onto a resource.
TIMEOUT can be "inf" meaning don't timeout. BUSY can be "rand" meaning a random busy-wait between 0 and 1 seconds.
Specify DEBUG=1 ruby pool.rb
to see each request acquire and release its resource.