Here's one, fairly simple way to do what you've requested.

use strict; use warnings; use threads; sub getArray { my( $arg1, $arg2, $fn ) = @_; my @array = 'a' .. 'z'; return @array; } sub getHash { my( $arg1, $arg2, $arg3, $fn ) = @_; my( $key, $value ) = ( 'A', 1 ); return map{ $key++ => $value++ } 1 .. 10; } my( $arg1, $arg2, $arg3, $filename ) = @ARGV; ### Note: The function that is run in the thread ### is called in the same context as the call to create. ### If you want the function called in a llist context, ### you must call create in a list context. my( $thrArray ) = threads->create( \&getArray, $arg1, $arg2, $filename + ); my( $thrHash ) = threads->create( \&getHash, $arg1, $arg2, $arg3, $fil +ename ); ### Do anything else the main thread needs to do here, *before* callin +g join. ### The joins will block until their threads complete. ### The values returned by the functions are returned when you join th +e thread. my @array = $thrArray->join; my %hash = $thrHash->join; print "@array\n"; print map( "$_:$hash{ $_ }, ", sort keys %hash ), $/; __END__ C:\test>533816 One Two Three apath\to\afile a b c d e f g h i j k l m n o p q r s t u v w x y z A:1, B:2, C:3, D:4, E:5, F:6, G:7, H:8, I:9, J:10,

This is the very simplest use of threads that avoids the need for shared data completely. Unless your subroutines do IO, or you are using a multi-cpu machine, this will not speed up your program.

On a single cpu machine, if they do both do IO (to or from different files!), it may speed it up somewhat, depending upon the performance of your filesystem and hardware and the proportion of time the code spends waiting for IO relative to do cpu-intensive code, but it may not.

On a multi-cpu machine, it should run more quickly.


Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
Lingua non convalesco, consenesco et abolesco. -- Rule 1 has a caveat! -- Who broke the cabal?
"Science is about questioning the status quo. Questioning authority".
In the absence of evidence, opinion is indistinguishable from prejudice.

In reply to Re: Howto capture array/hash back after threaded process by BrowserUk
in thread Howto capture array/hash back after threaded process by neversaint

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.