chrestomanci has asked for the wisdom of the Perl Monks concerning the following question:
Greetings wise brothers.
I am working on a multi threaded perl script (using a Thread_pool design pattern), where I have one thread building a queue of work to be done, and a number of worker threads pulling items of work from the queue.
In order to keep the queue size sensible, and the workers fully employed, I am using a Thread::Semaphore to control the work finding thread. If the queue becomes full then the work finding thread calls down() on the semaphore to block untill it is signaled to resume. If a worker thread finds the queue to be less than half full it calls up() on the semaphore to wake up the work finding thread to keep the queue of work full.
My problem is that Thread::Semaphore stores an integer (equal to the number of up() calls less the number of down() calls), and not a boolean value. I have a large number or worker threads and they can make lots of up() calls driving the value in the semaphore to a large number, so that it takes a lot of down() calls before the semaphore blocks.
What I would like is a semaphore that contains a boolean 1 or 0 value, so that any number of up() calls will not increase the value beyond 1, and every call to down() will always block untill another thread calls up.
Is there a way of using Thread::Semaphore in this way, or an alternative semaphore module that can be used for this purpose?
|
---|
Replies are listed 'Best First'. | |
---|---|
Re: Boolean Thread::Semaphore ?
by BrowserUk (Patriarch) on Feb 08, 2012 at 12:14 UTC | |
by chrestomanci (Priest) on Feb 08, 2012 at 16:10 UTC | |
by mellon85 (Monk) on Feb 08, 2012 at 17:28 UTC | |
by BrowserUk (Patriarch) on Feb 08, 2012 at 19:00 UTC | |
Re: Boolean Thread::Semaphore ?
by Corion (Patriarch) on Feb 08, 2012 at 12:04 UTC | |
by chrestomanci (Priest) on Feb 08, 2012 at 12:12 UTC | |
Re: Boolean Thread::Semaphore ?
by sundialsvc4 (Abbot) on Feb 08, 2012 at 21:52 UTC | |
Re: Boolean Thread::Semaphore ?
by Anonymous Monk on Feb 09, 2012 at 14:40 UTC | |
by BrowserUk (Patriarch) on Feb 09, 2012 at 19:33 UTC | |
by chrestomanci (Priest) on Feb 09, 2012 at 16:25 UTC |