From: Robert Klemme Date: 2004-02-09T19:04:59+09:00 Subject: Re: Multi-threading lesson wanted "Charles Comstock" schrieb im Newsbeitrag news:c07jo8$l5f$1@newsreader.wustl.edu... > Robert Klemme wrote: > > > "Tim Bates" schrieb im Newsbeitrag > > news:4025CA70.9090202@bates.id.au... > > > > > >>Below is the code I have written. The idea is that other application > >>code calls SAMS::Database.get_handle{ |handle| handle.execute... } and > >>this module takes care of the rest. Also, the application's shutdown > >>code will call SAMS::Database.destroy to cleanly disconnect all the > > > > handles. > > > > You definitely need some kind of synchronization mechanism that makes > > accesses to the shared connection pool thread safe. In your case a > > ConditionVariable will help you with the max connection logic. > > > > See section "Logging from multiple threads" for example usage of a > > ConditionVariable at > > http://www.rubygarden.org/ruby?MultiThreading > > > > I'd implement a Semaphore using a ConditionVariable and a Mutex; that way > > you can initialize the semaphore with the max connection value and count > > the semaphore down when you take a connection from the pool and increment > > it when you put it back. If the semaphore is zero, the next thread trying > > to decrease the semaphore is put to sleep and will wake up when another > > thread increments the semaphore. > > > > You can as well use the semaphore implementation in the RAA: > > http://raa.ruby-lang.org/list.rhtml?name=semaphore > > > > > > > > As an additional note: I would not use module methods for the pool > > handling. Instead I'd instantiate an instance of the pool. This is IMHO > > better since the pool is not necessarily a singleton: Just think of an > > application that needs to access more than one database and hence use more > > than one connection configuration. > > > > Kind regards > > > > robert > > > Why doesn't the counting semaphore library come standard with Ruby? Dunno. > I > haven't done anything multithreaded in a little while, but I remember > being annoyed I didn't have a counting semaphore. Is there a particular > reason it's not part of the standard library? Obviously not everything > should come standard but this seems to be the sort of thing that should. Yeah, that's true. +1 for including Semaphore in "Thread". Matz? Regards robert