Anonymous Monk:

Your script is actually very close to working. The only problem is that you didn't properly set the context for the thread. (Read perldoc threads and look for the THREAD CONTEXT section. Since you didn't capture the return value of the create() call, you set up the context of the thread as VOID context, meaning that you can't fetch the return code, and that's why you're getting an undefined value later when you call join().

So for you to fix your code, you need only set the appropriate context for the thread. Since you want a scalar result, you need only capture the result of the create() call into a scalar. That way, when you actually call the join() method, it will fetch the scalar result for you. You don't need to keep the scalar, so the simple fix for your program is to change the loop that creates your worker threads from:

for ( 1..2 ) { threads -> create ( \&worker ); }

to this:

for ( 1..2 ) { my $temp_scalar = threads -> create ( \&worker ); }

Afterwards, when you run your script, you'll get the result:

$ perl pm_1228826.pl sum=1 sum=2

Here, I used the 'implicit' method of setting the context. However, if you start building larger applications with different types of worker threads, you might find the explicit method of setting the context to be better. Just be sure to review the entire threads documentation and look for unexpected things. You'll also want to look over the examples and/or test code for the threads, threads::shared and Thread::Queue modules to see how their designers expect them to be used.

Please learn how to properly quote code in a node so that people can read it more easily. I've reformatted it, made the fix I mentioned earlier, and quoted it here for other readers:

use strict; use warnings; use threads; use Thread::Queue; my $process_q = Thread::Queue -> new(1,2); sub worker { while ( $process_q -> dequeue() ) { my $retVar=threads -> self() -> tid(); return $retVar; } } $process_q -> end(); #start some threads for ( 1..2 ) { my $tmp = threads -> create ( \&worker ); } #Wait for threads to all finish processing. foreach my $thr ( threads -> list() ) { my $sum=$thr -> join; print "sum=$sum\n"; }

EDIT: Looks like the janitors cleaned up the formatting in the parent node before I replied.

...roboticus

When your only tool is a hammer, all problems look like your thumb.


In reply to Re^2: A basic 'worker' threading example. by roboticus
in thread A basic 'worker' threading example. by Preceptor

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.