Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling
 
PerlMonks  

Re: How do I queue perl subroutines to a thread queue instead of data?

by Random_Walk (Prior)
on Apr 25, 2013 at 14:03 UTC ( [id://1030676]=note: print w/replies, xml ) Need Help??


in reply to How do I queue perl subroutines to a thread queue instead of data?

If I just had two threads to signal I would create a queue for each one and just enqueue the messages when I felt the need. the thread can then block on a queue read and do nothing until it gets a message

Updated to mention threads->create

... use Thread::Queue; my $conn_q = thread::queue->new() my $rev_q = thread::queue->new() sub connection_check { while (my $msg = $conn_q->dequeue) { # will block 'til it gets sign +aled # Do something } } ... # in main $conn_thread = threads->create('connection_check'); $conn_q->enqueue("Get on with it");

$min % 55 will be zero at 55 and at 0 minutes. Perhaps two timers would do your trick

my %timer = ( check_conn => 0, check_rev => 0, }; my %interval = ( check_conn => 5, # check conn every 5 min check_rev => 55, # and revocation at 55min }; while (1) { my $now = time; if ($now + interval{check_conn} * 60 > $timmer{check_conn} ) { #pa +ssed mark $timer{check_conn} = $now + interval{check_conn} * 60; # signal conn thread to do its work $conn_q->enqueue("Get on with it"); } # similar for check rev list # sleep 10; # or 60, or 300... }

Cheers,
R.

Pereant, qui ante nos nostra dixerunt!

Replies are listed 'Best First'.
Re^2: How do I queue perl subroutines to a thread queue instead of data?
by static0verdrive (Initiate) on Apr 25, 2013 at 14:43 UTC
    Thanks for your reply! OK I think I'm starting to get it... are you saying that I never thread->create but instead only $conn_q->enqueue in the #main?

    In other words, my $conc_q = thread::queue->new() would exist at the top and no thread->create would exist anywhere because the enqueue replaces it?

      No Sorry! you still need to thread->create! Just make a thread that listens in the queue with the while (my $msg = $q->dequeue) { ... and it will wait until signalled. You can just use this to hold it up or you can check the contents of $msg and behave accordingly

      Cheers,
      R.

      Pereant, qui ante nos nostra dixerunt!
        Understood - awesome, ty!! So I would want my threads->create() calls outside / just before the while loop in #main so they get created, but then inside the loop, based on when it's time to run, I queue them up! (right?) Tiny light bulb flickering on....

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others taking refuge in the Monastery: (5)
As of 2024-03-28 13:31 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found