in reply to Re: multi-threaded questions
in thread multi-threaded questions

Do you have an idea if I am doing the que stuff properly?

Replies are listed 'Best First'.
Re^3: multi-threaded questions
by renodino (Curate) on Dec 08, 2006 at 20:30 UTC
    See embedded comments:
    use Thread::Queue; # # Thread::Queue is already shared (in fact, # its just a shared array) # our $queue = Thread::Queue->new; sub thread_sub { while ($test == 0) { if (certain test is true) {$test = 1;} } # # Not certain of your intent here, but assuming you're # just trying to send some data to another thread, then # you just need to pass the data as a parameter; # Note that doing so will return immediately wo/ # any ack. from the recv'r. Also note that # if $action is a ref to something, then that # something must also be threads::shared # # {my $action = $queue->enqueue;} $queue->enqueue($action); } sub thread_sub1 { # # this is fine. # $action = $queue->dequeue; #??? totally lost here print "I am going to do your bidding master now that the test is + true\n"; }
    Blatant plug:

    If you need full duplex communications between threads, you might peek at Thread::Queue::Duplex or Thread::Queue::Multiplex.


    Perl Contrarian & SQL fanboy
      Thanks for your response. I dont even want to pass data to the other threads - I just want them to start moving at the calling of the first threads.

      Does the code need to change at all if looped as the original example suggests?

      Brian
        I don't really know what your "example suggests", but I'll assume you mean you want a persistent daemon that spins on the tests, and fires off a msg to the subordinate threads when there's work to do. One issue: your text describes 30 master threads dispatching to 30 worker threads. Does that mean the master thread only dispatches a single worker thread, or dispatches to all 30 ? If the former, your code is OK (assuming you move the enqueue() inside the loop). If the latter, you've got some challenges, and may find Thread::Queue::Multiplex useful.

        Also, while your Thread::Queue use is a convenient solution, you may find Thread::Semaphore a more "CompSci"-ish solution, and maybe apply the recent addition of inter-thread signalling, assuming you're using a shiny new version of the threads module.


        Perl Contrarian & SQL fanboy