Before I start, I will just say I understand that threads are not very stable as it is, let alone on win32 systems. However, I've got this belief that maybe I am doing something completely wrong and that the main problem this time around is not to be blamed on threads. I am just throwing together a simple HTTP proxy, the barebone code follows below. The code works fine if you only allow one connection at a time, but that makes things far too slow: so naturally, I turned to threading. Each time a new thread is created, an extra few thousand K of memory is taken up and never released (the thread itself is destroyed; task manager shows the thread count drop). Am I supposed to be using one or more of threads' detach(), yield(), join() methods? I've tried tossing them in multiple places and it always just gives me even more problems!

#!c:/perl/bin/perl -w $| = 1; use strict; use threads; use IO::Socket; use HTTP::Request; use LWP::UserAgent; my $proxy = IO::Socket::INET->new( Listen => 1, LocalAddr => '127.0.0.1', LocalPort => 8090, ReuseAddr => 1 ) or die( "Failed to start HTTP proxy: $!" ); while ( my $client = $proxy->accept() ) { threads->create( 'new_request', $client ); } sub new_request { my $client = shift; my $page = <$client>; $page =~ s![\015\012]+\z!!; my ($method, $server, $url, $http_v) = $page =~ m!\A(GET|POST|HEAD)\s+http://([^/]+)(/.*)?\s+HTTP/(1.[01])\ +z!i; $url ||= '/'; my %headers; while ( my $line = <$client> ) { goto( DISCONNECT ) unless ( defined($line) ); $line =~ s![\015\012]+\z!!; last if ( $line eq "" ); my ($k, $v) = $line =~ m!\A([^:]+):\s*(.+)\z!; $headers{ lc $k } = $v; } my $ua = LWP::UserAgent->new(); my $req = HTTP::Request->new(); $req->method( $method ); $req->uri( "http://$server$url" ); $req->header( $_ => $headers{$_} ) for ( keys %headers ); if ( exists( $headers{'content-length'} ) ) { read( $client, my $buf, $headers{'content-length'} ); $req->content( $buf ); } my $res = $ua->request( $req ); print $client "HTTP/$http_v ", $res->status_line(), "\015\012"; $res->scan( sub { print $client "$_[0]: $_[1]\015\012"; } ); print $client "\015\012", $res->content(); DISCONNECT: shutdown( $client, 2 ); return(); }

In reply to threads hogging memory by saskaqueer

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.