Problem with memory when using "threads" with Perl 5.8 on Windows System

G

Gavin Williams

I am working on a multi-threaded server for a Windows 2000 system and since
"fork" doesn't work that great in a Win32 environment, I am trying to use
use the "threads" module instead.

When a thread is complete, the handle and thread are removed but the memory
used when the thread was initially created does not appear to be released.
Any new threads created afterward will grab new memory on top of where the
previous left off.

This is a problem because what I am trying to do is create a new thread with
each new client connection, and then remove the thread when the client
disconnects. Clearly, I will run out of memory after several client
connections if it doesn't release.

Any one have a suggestion?

#####################################################

use IO::Socket ;
use threads ;
use strict ;


sub AcceptClient {

print "Starting new thread for new client...\n" ;

my $buf ;
my $handle = shift ;
while ( defined ( $buf = <$handle>)) {
chomp $buf ;
print "Received <$buf>\n" ;

# interpret $buf as client commands and process them here...
# ...
}

print "Closing new socket\n" ;
shutdown ( $handle , 2 ) ;
undef $handle ;

print "New thread is done...\n\n" ;

return 1 ;
}

my $main_sock = IO::Socket::INET->new( LocalPort => 9501 , Listen => 5,
Reuse => 1 , Proto => 'tcp' );
die "Socket could not be created. Reason: $^E \n" unless ( $main_sock ) ;

print "Listening for new connections...\n" ;
my $new_sock ;

while ( $new_sock = $main_sock->accept() ) {
print "New connection accepted...\n" ;

my $thread = threads->create( \&AcceptClient , $new_sock ) ;

$thread->detach ;

}

shutdown($main_sock,2) ;
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,009
Latest member
GidgetGamb

Latest Threads

Top