While others have commented on alternative methods to the overarching problem, I wanted to address the
specific question of a 'boolean' semaphore. One 'brain-dead' simple approach
would be to sub-class Thread::Semaphore such that the ->up() operation sets
the semaphore to 1 rather than incrementing it.
package Thread::Semaphore::Boolean;
use threads::shared;
use parent 'Thread::Semaphore';
# Create a new 'boolean' semaphore
sub new {
my $class = shift;
return $class->SUPER::new(1);
}
# Set semaphore's count to 1
sub up {
my $sema = shift;
lock($$sema);
$$sema = 1;
cond_broadcast($$sema);
}
1;
Again, this is 'brain-dead' in that elementary uses of ->down() and ->up()
will work, whereas other 'fancy' operations may break (e.g., $sema->down(2) would 'hang' because
the 'boolean' semaphore would never be greater than 1).