Moving the example scripts here since it was spamming the main page, sorry :)

The RCVR_MAXCON variable in the example script is set to 3 to make this happen much quicker.

#!/usr/bin/perl -w use strict; use threads; use threads::shared; use IO::Socket; my $RCVR_MAXCON = 3; # Max connections a receiver will process before +recycling my $MAXRCVR = 50; # Hard max limit my $MINIDLE = 5; # Minimum idle threads my $MAXIDLE = 10; # Maximum idle threads # Shared receiver thread status hash my %idle : shared; my %busy : shared; my $statchg : shared = 1; # change status sub setidle { my $tid = threads->self->tid; lock($statchg); delete $busy{$tid}; $idle{$tid} = 1; $statchg++; cond_signal($statchg); } sub setbusy { my $tid = threads->self->tid; lock($statchg); delete $idle{$tid}; $busy{$tid} = 1; $statchg++; cond_signal($statchg); } sub setdead { my $tid = threads->self->tid; lock($statchg); delete $idle{$tid}; delete $busy{$tid}; $statchg++; cond_signal($statchg); } sub receiver($) { my $lsock = shift; my $runcount = $RCVR_MAXCON; threads->self->detach; while($runcount > 0) { my $data; my $temp; setidle; my $conn = $lsock->accept; setbusy; while($conn->read($temp,16384)) { $data.=$temp; } print "Thread " . threads->self->tid . " received $data\n"; $conn->close(); $runcount--; } print "+++Thread " . threads->self->tid . " exiting after $RCVR_MAXC +ON connections\n"; $lsock->close; setdead; } # Create the tcp socket; my $lsock = IO::Socket::INET->new ( Listen => 10, LocalPort => 7676, Proto => 'tcp', Reuse => 1 ) || die $!; while (1) { lock($statchg); cond_wait($statchg) until $statchg > 0; my $idlecount = scalar(keys %idle); my $busycount = scalar(keys %busy); my $total = $idlecount+$busycount; print "--Current-- Idle: $idlecount Busy: $busycount\n"; if($total < $MAXRCVR && $idlecount < $MINIDLE) { print "--------- Adding 1 thread!\n"; threads->create("receiver",$lsock) || die $!; } elsif($idlecount > $MAXIDLE) { print "--------- Killing 1 thread!\n"; # kill 1 thread, unimplemented yet... } $statchg=0; }
And here's a client script I use to stuff some connections at the server. I usually just fire off several in the background like: ./testclient & ./testclient & ./testclient &
#!/usr/bin/perl -w use strict; use IO::Socket; while(my $sock = IO::Socket::INET->new("localhost:7676")) { print $sock "TEST"; sleep 1; $sock->close; }

In reply to Re: Problems with a thread-pooled tcp server by ph713
in thread Problems with a thread-pooled tcp server by ph713

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.