in reply to Re: multithreading sample needed
in thread multithreading sample needed

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 :/

Replies are listed 'Best First'.
Re^3: multithreading sample needed
by roboticus (Chancellor) on May 01, 2012 at 18:33 UTC

    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.

Re^3: multithreading sample needed
by BrowserUk (Patriarch) on May 01, 2012 at 18:44 UTC

    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?

Re^3: multithreading sample needed
by zentara (Cardinal) on May 02, 2012 at 10:31 UTC
    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