in reply to Re^2: Thread creation failed: pthread_create returned 11
in thread Thread creation failed: pthread_create returned 11
You are simply exhausting your memory because you are never joining your threads.
The easiest fix is to just detach your threads so they get cleaned up automatically. (Also, there is no point in calling threads->exit; better to just fall off the end.
So whilst your example above chews up 8GB of memory in around a minute or so, this slightly modified version runs with no signs of memory growth at all:
#! perl -sw #use strict; use threads; use threads::shared; my $tcount :shared = 0; while(1){ if($tcount < 4){ print("Number of active threads : ".$tcount." processing incom +ming message\n"); threads->create(processData)->detach; } else { print("Maximum number of threads reached. Waiting \n"); sleep 1 until $tcount < 4; threads->create(processData)->detach; } } sub processData { $tcount++; #sleep(2); print "OK \n"; $tcount--; }
There are several other bad things and weirdnesses in that short snippet -- most of which would be cured by making it strict/warnings safe; but I guess that request fell on deaf ears -- but it will cure your headline problem.
|
|---|