Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change

Re^2: Threading in a loop

by jerre_111 (Sexton)
on Apr 22, 2013 at 13:56 UTC ( [id://1029879]=note: print w/replies, xml ) Need Help??

in reply to Re: Threading in a loop
in thread Threading in a loop

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


Replies are listed 'Best First'.
Re^3: Threading in a loop
by hdb (Monsignor) on Apr 22, 2013 at 14:11 UTC

    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!

        Is that not expected? When you asynchronously increment the variable and print the variable it will not be in line. There is nothing in the code to synchronize the two.

Re^3: Threading in a loop
by CountOrlok (Friar) on Apr 22, 2013 at 14:10 UTC
    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.

Log In?

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

How do I use this?Last hourOther CB clients
Other Users?
Others rifling through the Monastery: (2)
As of 2024-05-22 22:23 GMT
Find Nodes?
    Voting Booth?

    No recent polls found