First, just wanted to point out that the documentation for the Thread module indicates that it is deprecated and recommends users to use the threads module instead (see DEPRECATED section). That has nothing to do with your question, but might help you to avoid other issues down the road.

For creating a log of everything from all of the threads, you could create a variable in each thread that would contain the log contents of that thread and have the thread return that value. Then the main code section can print that out to a file afterwards.

Here's an untested modification of your code that will does what I just described.

#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 { my $log = ""; 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($_)) +; $log .= "Thread 1: Doing SNMP GET for $_\n"; } } return $log; }; my $thr2 = async { my $log = ""; 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($_)) +; $log .= "Thread 2: Doing SNMP GET for $_\n"; } } return $log; }; my $log1 = $thr1->join; my $log2 = $thr2->join; my $file = 'threadinfo.txt' open(my $output,">",$file) or die "Unable to open file '$file': $!"; print $output $log1; print $output $log2; close($output);

Of course, one drawback on that would be that if a thread were to die prematurely, you would lose all logging for that thread. Since you are already using a shared variable for the threads to read from, you could also use one or more shared variables that the threads could use for logging. And again, write that out to file after joining all of the threads.

And if you are needing/wanting the logging to be intermixed, just add a timestamp for each log entry from each thread. Then you could sort the entries from all of the logs afterwards to create a single chronologically ordered log from the contents of all of the thread logs.


In reply to Re: Redirecting output in Windows cmd prevents second thread from doing anything by dasgar
in thread 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.