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.
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
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |