in reply to Howto capture array/hash back after threaded process

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.

Replies are listed 'Best First'.
Re^2: Howto capture array/hash back after threaded process
by neversaint (Deacon) on Mar 03, 2006 at 01:45 UTC
    On a multi-cpu machine, it should run more quickly.
    Dear BrowserUk,
    Thanks so much for the reply. I manage to get it working. Few question however, to clarify my understanding of your statement above.
    • Those number of CPU we have is the one shown through this command right?
      $ cat /proc/cpuinfo
    • What happen if the number of process to be threaded is greater than the number of CPU we have? Will they be evenly distributed? Still can we benefit from the speedup?


    ---
    neversaint and everlastingly indebted.......
      Those number of CPU we have is the one shown through this command right? $ cat /proc/cpuinfo

      I don't use *nix, so you'll need get a response from one of the many here that do.

      What happen if the number of process to be threaded is greater than the number of CPU we have?

      If you have (say) 2 cpus and 3 cpu-bound functions running in threads, then they will complete more quickly, than running the same 3 functions one after the other as a single threaded process.

      It may be quicker overall to only run two threads as the same time to avoid excessive task switching, but that it a fine tuning thing best left for when you see what performance you achieve doing the simple thing.

      Will they be evenly distributed?

      That is actually quite hard to answer accurately without knowing what the processes are doing; what else is running on the system; etc., but in most cases the answer will be: Yes.

      Still can we benefit from the speedup?

      Again, if you described what these threads will be doing you will get a more accurate and definitive answer, but under most OSs and circumstances: Yes.


      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.