Beefy Boxes and Bandwidth Generously Provided by pair Networks
Come for the quick hacks, stay for the epiphanies.
 
PerlMonks  

Re: Boolean Thread::Semaphore ?

by BrowserUk (Patriarch)
on Feb 08, 2012 at 12:14 UTC ( [id://952473]=note: print w/replies, xml ) Need Help??


in reply to Boolean Thread::Semaphore ?

Is there a way of using Thread::Semaphore in this way, or an alternative semaphore module that can be used for this purpose?

You don't want a semaphore.

The simplest mechanism is:

wbile( my $workitem = sourceOfWork() ) { sleep 1 while $Q->pending > MAXQ; $Q->enqueue( $workitem ); }

If you are concerned that the queue might empty during that 1 second, use Time::HiRes::usleep().

If you really insist on not polling the queuesize, then use a condition vatiable:

my $cond :shared; ... wbile( my $workitem = sourceOfWork() ) { cond_wait( $cond ); $Q->enqueue( $workitem ); } ## somewhere else ... ... cond_signal( $cond ) if $Q->pending < MAXQ; ...

But really, the latter involves more work and is less effective.


With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
"Science is about questioning the status quo. Questioning authority".
In the absence of evidence, opinion is indistinguishable from prejudice.

The start of some sanity?

Replies are listed 'Best First'.
Re^2: Boolean Thread::Semaphore ?
by chrestomanci (Priest) on Feb 08, 2012 at 16:10 UTC

    You don't want a semaphore.

    The simplest mechanism is:

    while( my $workitem = sourceOfWork() ) { sleep 1 while $Q->pending > MAXQ; $Q->enqueue( $workitem ); }

    Thanks, that looks to be a much more sensible approach compared with what I was doing with semaphores.

    That should teach me to read algorithoms online that are intened for other languages or problem domains where near realtime responses are required.

      The best way (aka not polling) would be that the worker thread will stop itself if the queue is full, and will be restarted by a client thread by a signal.

      take a look at thread signalling chapter in threads documentation

        take a look at thread signalling chapter in threads documentation

        That is a truly awful idea.

        Try it for yourself to find out why.


        With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
        Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
        "Science is about questioning the status quo. Questioning authority".
        In the absence of evidence, opinion is indistinguishable from prejudice.

        The start of some sanity?

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://952473]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others having an uproarious good time at the Monastery: (3)
As of 2024-04-25 09:37 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found