Hi

If all you're doing is returning a value, you don't need to share anything

#!/usr/bin/perl -- use strict; use warnings; use threads stack_size => 4096; ## void context returns undef threads->create( \&worker, 'LocalA', 5 ); threads->create( \&worker, 'LocalB', 5 ); threads->create( \&worker, 'LocalC', 5 ); { ## list context returns return value my @a = threads->create( \&worker, 'LocalAlfa', 15 ); @a = threads->create( \&worker, 'LocalBravo', 15 ); @a = threads->create( \&worker, 'LocalCharlie', 15 ); } waiter(); exit( 0 ); sub worker { # require LocalMyApp; return LocalMyApp::DoTheJob(); my $obj = shift; my $sleeprand = shift; print join ' ', threads->tid, threads->self, $obj, $sleeprand, "\n"; my $rand = int(rand()*1000); sleep rand $sleeprand; return $rand; } sub waiter { while( threads->list ) { for my $joinable ( threads->list( threads::joinable ) ) { print join " ", $joinable->tid, $joinable, $joinable->join || 'undefined', ## return "\n"; } } } __END__ 1 threads=SCALAR(0xb7032c) LocalA 5 2 threads=SCALAR(0xbfc554) LocalB 5 3 threads=SCALAR(0xc84b14) LocalC 5 4 threads=SCALAR(0xd3443c) LocalAlfa 15 5 threads=SCALAR(0xdbca7c) LocalBravo 15 6 threads=SCALAR(0xe4c3f4) LocalCharlie 15 2 threads=SCALAR(0xa6d354) undefined 1 threads=SCALAR(0xa6d354) undefined 3 threads=SCALAR(0xa6d354) undefined 4 threads=SCALAR(0xa6d354) 560 6 threads=SCALAR(0xa6d354) 380 5 threads=SCALAR(0xa6d354) 552

In reply to Re: Threads inside Objects & Sharing (return doesnt need sharing) by Anonymous Monk
in thread Threads inside Objects & Sharing by Anonymous Monk

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.