469,658 Members | 1,855 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,658 developers. It's quick & easy.

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

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) ;
Jul 19 '05 #1
0 3360

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

2 posts views Thread by Michael Schutte | last post: by
10 posts views Thread by Drakier Dominaeus | last post: by
4 posts views Thread by David Travis | last post: by
4 posts views Thread by James A Taber | last post: by
94 posts views Thread by Samuel R. Neff | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.