sweepy838 has asked for the wisdom of the Perl Monks concerning the following question:

Hi i've read a bit on multithreading in perl, there's no sure way of doing it and that its still in development.. so i was wondering if there is a work-around? this isnt for a GUI. if possible i'd like a small example of how to achieve the follow code.
use threads; sub thread() { for (1..10) { print "hello"; } } for (1..10) { print "hello"; }
now if that was multithreading (its way off) it would print "hello \r\n word" at the same time, right? ive used multithreading in .net

Replies are listed 'Best First'.
Re: multithreading sample needed
by zentara (Cardinal) on May 01, 2012 at 17:28 UTC
    You don't make a thread just by calling a subroutine thread(), so you are way off, and not actually creating any threads at all. Here is what you probably are looking to do.

    P.S. It's actually more complicated in real life scripts, so don't be dazzled by this simplistic example. :-)

    #!/usr/bin/perl use warnings;; use strict; use threads; for (1..10) { print "hello $_ from main\n"; } for (1..10){ threads->create( \&thread, $_ )->detach; } print "all done, press the Enter key to exit\n"; <>; # this is done so the main thread dosn't exit before # the threads can finish sub thread { my $val = shift; foreach my $x (1..10) { print "hello $x from thread $val\n"; #select( undef,undef,undef,.5); #half second sleep } }

    I'm not really a human, but I play one on earth.
    Old Perl Programmer Haiku ................... flash japh
      this is all a bit odd, because from what i've read in perl threading is nothing like multithreading.. to me, and to other languages capable of multithreading it means 2 sections of code can run at the same time. 1 doing some job and the other doing some other job at the same time.. it seems that perl's idea of multithreading is just as good as calling the avarage subroutine :/

        sweepy838:

        No, it's working as it should. The sample provided by zentara simply doesn't show it to you, as each loop runs too fast to allow the next thread to start before it terminates. Uncomment the select statement inside the for loop, and you'll see that they can all run side-by-side. (I'm guessing he forgot to uncomment it before posting.)

        $ perl 968307.pl hello 1 from main hello 2 from main hello 3 from main hello 4 from main hello 5 from main hello 6 from main hello 7 from main hello 8 from main hello 9 from main hello 10 from main hello 1 from thread 1 hello 1 from thread 2 hello 1 from thread 3 hello 1 from thread 4 hello 1 from thread 5 hello 1 from thread 6 hello 1 from thread 7 hello 1 from thread 8 hello 1 from thread 9 all done, press the Enter key to exit hello 1 from thread 10 hello 2 from thread 1 hello 2 from thread 2 hello 2 from thread 3 hello 2 from thread 4 hello 2 from thread 5 hello 2 from thread 6 hello 2 from thread 7

        Update: Swapped a couple of sentences so it makes more sense.

        ...roboticus

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

        Perhaps you'll find this a more convincing demonstration of concurrency?

        Each of four threads continuously prints its own thread id (a number from 1.. 4) to the screen; while the 5th (main) thread waits for you to hit enter:

        perl -Mthreads -e"$|++;async{print threads->tid while 1}->detach for 1 + .. 4; <>"

        Switch "s for 's if you're on *nix.


        With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
        Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
        "Science is about questioning the status quo. Questioning authority".
        In the absence of evidence, opinion is indistinguishable from prejudice.

        The start of some sanity?

        Here is another way to slow things down for visualization. Add a delay in the thread sub.
        sub thread { my $val = shift; foreach my $x (1..10) { print "hello $x from thread $val\n"; my $delay = int rand 5; sleep $delay; } }
        it seems that perl's idea of multithreading is just as good as calling the avarage subroutine :/

        On a single cpu machine, that is true.

        In real world work, threading is more useful as a means of easily sharing variables between different code blocks running independently, using threads::shared. Otherwise, forking is usually a better parallel processing solution because of issues like memory reclamation, independence of IO, sharing objects, and a few others gotchas which threads occaisionally cause. For example, look how easy it is for the main thread and the child thread can share the array:

        #!/usr/bin/perl use warnings; use threads; use threads::shared; use strict; $|=1; #turn off buffering my @hosts_shared : shared; #declare as shared before setting value @hosts_shared = (1,2,3,4,5); print "@hosts_shared\n"; my $thread = threads->new(sub { print "Run the thread! array should be modified\n"; #modify shared array push @hosts_shared, 42; })->detach(); sleep 1; # cheap hack to allow thread time to work print "@hosts_shared\n"; print "Hit enter to exit\n"; <>;

        I'm not really a human, but I play one on earth.
        Old Perl Programmer Haiku ................... flash japh
Re: multithreading sample needed
by roboticus (Chancellor) on May 01, 2012 at 17:23 UTC
Re: multithreading sample needed
by locked_user sundialsvc4 (Abbot) on May 01, 2012 at 22:16 UTC

    Definitely also look at existing CPAN modules, such as Parallel::ForkManager.   There are many modules out there which are designed to address the rudiments of putting together a multi-threaded application.   Whether you actually use the modules in question, they are also a great resource as an example of complete and running code.