The objective of my code snippet is to spawn a couple of threads(two in this case) and have each thread send two cold start traps. I'm trying to build in a timing mechanism such that all the traps get sent within a specific amount of time (one second in this case). When you run the code you'll see my question. It seems that the first iteration of the second thread always fails due to a negative time calculation. I've tried locking the variables down until they go out of scope so they aren't stepping on each other, but this doesn't seem to work.

Any help or suggestions would be greatly appreciated.
Thanks!

#!C:/perl/bin/perl.exe use warnings; use strict; use threads; use threads::shared; use Net::SNMP; use Time::HiRes qw(usleep gettimeofday tv_interval); my $t0s : shared = 1; my $t1s : shared = 1; my $t0m : shared = 1; my $t1m : shared = 1; my $rest : shared = 1; my $elapsedtime : shared = 1; my $tmp : shared = 1; my $gen = 0; my $src = '18.42.246.82'; my $dest = '18.42.248.124'; my $iterations = 2; my $sec = 1000000; my $sleep = ( $sec / ($iterations * 2)); my $t0 = [gettimeofday]; my ($t0secs, $t0micro) = gettimeofday; my $thr = threads->create(\&sendTrap, $gen, $src, $dest, $iterations); my $thr2 = threads->create(\&sendTrap, $gen, $src, $dest, $iterations) +; $thr->join; $thr2->join; my $t1 = [gettimeofday]; my $elapsed = tv_interval $t0, $t1; print "\nTotal Elapsed Time: $elapsed\n"; # Should be close to one sec +cond sub sendTrap { my ($gen, $src, $dest, $i) = @_; for(1..$i) { ($t0s, $t0m) = gettimeofday; lock($t0m); my $oid = '1.3.6.1.4'; my @trapvars = (); my ($session, $error) = Net::SNMP->session( -hostname => $dest, -community => 'public', -port => 162 ); if (!defined($session)) { printf("ERROR: %s.\n", $error); exit 1; } my $result = $session->trap( -enterprise => $oid, -agentaddr => $src, -generictrap => $gen, -specifictrap => '0', -varbindlist => \@trapvars ); if (!defined($result)) { printf("ERROR: %s.\n", $session->error); $session->close; exit 1; } $session->close; ($t1s, $t1m) = gettimeofday; lock($t1m); lock($elapsedtime); $elapsedtime = $t1m - $t0m; if ($elapsedtime =~ /\-/) { lock($tmp); $tmp = $elapsed; $tmp =~ s/\-//; $elapsedtime = 1000000 - $tmp; } print "\n\nStart Sleep: $sleep\n"; print "Elapsed Time: $elapsedtime\n"; lock($rest); $rest = ($sleep - $elapsedtime); print "Net Sleep: $rest\n"; usleep $rest; } }

In reply to sleep and timing? by Anonymous Monk

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.