in reply to Data Between Threads

Simple example to get you started:

#! perl -slw use strict; use threads; use Thread::Queue; sub threadA { my $Qab = shift; for( 1 .. 1e6 ) { $Qab->enqueue( 1+int rand 1000 ); } $Qab->enqueue( undef ); } sub threadB { my( $Qab, $Qbc ) = @_; while( my $in = $Qab->dequeue() ) { $Qbc->enqueue( $in * 10 ); } $Qbc->enqueue( undef ); } my $Qab = new Thread::Queue; my $Qbc = new Thread::Queue; threads->new( \&threadA, $Qab )->detach; threads->new( \&threadB, $Qab, $Qbc )->detach; print while defined( $_ = $Qbc->dequeue ); print 'Done';
The main thread is your thread C.

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". I knew I was on the right track :)
In the absence of evidence, opinion is indistinguishable from prejudice.

Replies are listed 'Best First'.
Re^2: Data Between Threads
by UpTide (Novice) on Aug 17, 2016 at 16:09 UTC

    On the threadA you are putting shift into $Qab. What is this doing? I am assuming it is to put the parameter into the sub's $Qab, but correct me if I am wrong.

    Also what role does $Qab->enqueue( undef ); provide outside of the while loop? Shouldn't the queue be undefined while it is empty? If that is the case what is the advantage of explicitly enqueuing undef?

    Thanks everyone for your valuable time!

      On the threadA you are putting shift into $Qab. What is this doing? I am assuming it is to put the parameter into the sub's $Qab, but correct me if I am wrong.

      Correct. A thread function is just a normal function that takes it arguments in the normal way.

      Also what role does $Qab->enqueue( undef );

      Once threadA has finished its loop; it queues undef before terminating. That undef causes the while loop in threadB to terminate; whence it then enqueues undef which cause the while loop in threadC to terminate.


      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". I knew I was on the right track :)
      In the absence of evidence, opinion is indistinguishable from prejudice.

      The shift function in a subroutine takes the first item from the parameter list @_, which in threadA's case is the Thread::Queue object $Qab. It was put there by the threads->new(...) method call. threadB has two parameters, so he used an alternate syntax for that. He could have written:

      sub threadB { my $Qab = shift; my $Qbc = shift; ...

      He puts the undef value in the queue as a way of indicating to whatever process uses the queue that it has reached the last value. When threadB's while loop gets this value, it will terminate. If this value was not there, the dequeue method would keep waiting for a value.

      But God demonstrates His own love toward us, in that while we were yet sinners, Christ died for us. Romans 5:8 (NASB)