Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic
 
PerlMonks  

Re: Threading in a loop

by choroba (Abbot)
on Apr 22, 2013 at 13:47 UTC ( #1029875=note: print w/ replies, xml ) Need Help??


in reply to Threading in a loop

If you want the thread to print the value more times, you have to include a loop in the thread. As written, the thread is started, it prints the value and ends.

لսႽ ᥲᥒ⚪⟊Ⴙᘓᖇ Ꮅᘓᖇ⎱ Ⴙᥲ𝇋ƙᘓᖇ


Comment on Re: Threading in a loop
Re^2: Threading in a loop
by jerre_111 (Sexton) on Apr 22, 2013 at 13:56 UTC

    Yes, does the trick. So basicly, you have to restart the thread every time?

    Thanks!

      I think a better way to put it is that you are going to create or spawn a new thread (not restart the thread).

      You would have to think some more about how to print every time the shared variable changes value.

      Your program finishes too fast. Try this:

      use threads; use threads::shared; my $test :shared; $test = 0; testing_thread(); sub testing_thread { my $thr1 = threads->create(\&progress_count, $test); while ($test <= 1000000) { $test += 1; } $thr1->join(); } sub progress_count { while( $test < 1000000 ) { print $test, " \n"; } }

        I'm sorry to say, but your code is not quite right.

        When you do it like that, the printing and counting of the variable won't run parallel. Resulting in seeing only fragments of the counting

        But thanks for the effort!

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1029875]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others wandering the Monastery: (6)
As of 2014-11-21 02:42 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My preferred Perl binaries come from:














    Results (104 votes), past polls