Hello, I have written a short perl script that creates two threads using async. There is a shared array @array, with this script I expect each thread to first lock @array, then pop out 10 elements from @arrays, push them into @my_oids, release the lock and later print each element in @my_oids. Both the threads keep doing this as long as there are elements left in the array @array. I have written this script on windows and execute it using the cmd shell. The output shows that the script appears to be running fine and both the threads are doing part of the work.
C:\Users\perluser09\Desktop\Perl>perl lock.pl ... Thread 1: Doing SNMP GET for 2.1.1.1:1.3.1.8.47 Thread 1: Doing SNMP GET for 2.1.1.1:1.3.1.8.46 Thread 1: Doing SNMP GET for 2.1.1.1:1.3.1.8.45 Thread 2: Doing SNMP GET for 2.1.1.1:1.3.1.8.40 Thread 1: Doing SNMP GET for 2.1.1.1:1.3.1.8.44 Thread 2: Doing SNMP GET for 2.1.1.1:1.3.1.8.39 Thread 2: Doing SNMP GET for 2.1.1.1:1.3.1.8.38 Thread 1: Doing SNMP GET for 2.1.1.1:1.3.1.8.43 ..... Perl exited with active threads 0 running and unjoined 2 finished and unjoined 0 running and detached
But, when I try to redirect the output of the script to a file and run the perl script as below, Thread 1 seems to do all the jobs and Thread 2 is missing completely.
C:\Users\perluser09\Desktop\Perl>perl lock.pl > threadinfo.txt .... Thread 1: size of the array is 199 Thread 1: Doing SNMP GET for 2.1.1.1:1.3.1.8.200 Thread 1: Doing SNMP GET for 2.1.1.1:1.3.1.8.199 Thread 1: Doing SNMP GET for 2.1.1.1:1.3.1.8.198 Thread 1: Doing SNMP GET for 2.1.1.1:1.3.1.8.197 Thread 1: Doing SNMP GET for 2.1.1.1:1.3.1.8.196 Thread 1: Doing SNMP GET for 2.1.1.1:1.3.1.8.195 Thread 1: Doing SNMP GET for 2.1.1.1:1.3.1.8.194 Thread 1: Doing SNMP GET for 2.1.1.1:1.3.1.8.193 Thread 1: Doing SNMP GET for 2.1.1.1:1.3.1.8.192 Thread 1: Doing SNMP GET for 2.1.1.1:1.3.1.8.191 ...... Perl exited with active threads: 0 running and unjoined 2 finished and unjoined 0 running and detached
What I discovered is that when I do not do any redirection, the two threads are created almost one after another, but when I use redirection, the second thread is created only after the first thread has completed the while loop, which means performed the entire operation. I would be grateful if someone would point out the reason for such behavior. The full code for the script is:
#create the array with the ip:OID use warnings; use strict; use Thread qw(async); use threads::shared; share (my @array); @array = qw/2.1.1.1:1.3.1.8.1 2.1.1.1:1.3.1.8.2 2.1.1.1:1.3.1.8.3 +2.1.1.1:1.3.1.8.4 2.1.1.1:1.3.1.8.5 2.1.1.1:1.3.1.8.6 2.1.1.1:1.3.1.8 +.7....sequence continues upto....2.1.1.1:1.3.1.8.199 2.1.1.1:1.3.1.8. +200/; my $thr1 = async { while($#array > 0) { print "Thread 1: size of the array is $#array\n"; my @my_oids; { lock (@array); # Block until we get access to $a for(my $i=0;$i<10;$i++) { push (@my_oids, (pop @array)); } } foreach(@my_oids) { print "Thread 1: Doing SNMP GET for $_\n" if (defined( +$_)); } } }; my $thr2 = async { while($#array > 0) { print "Thread 2: Size of the array is $#array\n"; my @my_oids; { lock (@array); # Block until we get access to $a for(my $i=0;$i<10;$i++) { push (@my_oids, (pop @array)); } } foreach(@my_oids) { print "Thread 2: Doing SNMP GET for $_\n" if (defined( +$_)); } } }; $thr1->join; $thr2->join;

In reply to Redirecting output in Windows cmd prevents second thread from doing anything by perluser09

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.