http://www.perlmonks.org?node_id=1029887


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

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!

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

    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.

      When you execute the following code you get a synchronised counting/printing action

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

        What you are doing makes no sense.

        And the way you are doing it makes no sense. This:

        while ($test <= 100) { my $thr1 = threads->create(\&progress_count, $test); $test += 1; $thr1->join(); }

        Is (unreliably) equivalent to doing this:

        while ($test <= 100) { $test += 1; print $test; }

        Except that:

        1. it is is vastly more inefficient;

          Because you are building and running and then throwing away a whole new thread in order to execute that single print statement.

          Which is a complete nonsense as it has no advantages and masses of disadvantages.

        2. it is unreliable;

          Because there is no guarantee one way or the other that by the time the thread has been built and the print statement is executed, whether the increment of the variable will have been done or not.

        That is, the while loop cannot continue until the print statement (in the thread) has run; so nothing is being "done in parallel".

        It doesn't even make sense as a beginners first exploratory step in using threads because it is like trying to test whether a bone china cup can safely contain concentrated sulphuric acid; because regardless of the outcome of the test, nothing useful is being done by doing so.

        If you think you have a real application for threading and are stuck on how to do it; describe it and we'll try to help you.

        If not, stop playing; because your misunderstanding is such that all you will do is acquire knowledge of how not to use threads.


        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.
        This will not always be synchronized. It is possible that in loop "n" it will print $test after the increment and in loop "n+1" it will print before the increment, thus printing the same number twice. Similarly, it could skip a number.
        Trying to synchronize threads by creating large numbers of threads with small scope is probably not the way to go.

        You code above has the added disadvantage of possibly printing some values twice while skipping a value later on.

        Wise synchronization of threads will usually involve either queues or semaphores. Feel free to look into Thread::Queue or Thread::Semaphore for implementation details.