I didn't understand what you wrote about my code, since threadscount is locked until it's increased and $count=$threadscount. Could you perhaps explain this ?
If main creates a new thread, then it exists, and therefore is consuming memory, before it can possibly increment $threadscount. So, if a thread gets a lock on $threadcount, and then gets swapped out holding that lock, and main gets a timeslice, then main will go into a tight loop creating 300 threads. Each of those threads. when it gets a timeslice, will attempt to get a lock on $threadscount, but there is still a lock held by another thread, so they will block.
The result is, you've just created 300 new threads that each occupy memory, but that will never show up in the $maxthreads count, because they are all blocked from incrementing $threadscount.
Actually, that is only one of several scenarios in your code that would lead to $maxthreads not reflecting the true situation.
Could you try this also? It absolutely guarentees that there are never more that $MAX threads (default=10) threads running concurrently. It uses the thread id of the latest thread created to monitor how many threads have been created so far.
On my system with $MAX set to 10, the memory usage wobbles a (very) little either side of 6MB, but even after 100,000+ creation/join cycles, it never displays any sign of long term memory growth.
use threads;
use threads::shared;
our $MAX ||= 10;
$|++;
my @threads;
while( 1 ) {
push @threads, threads->create( sub{ 1; } ) while @threads < $MAX;
printf "\rCreated so far; %d\t", $threads[ -1 ]->tid;
$_->join for @threads;
@threads = ();
}
What do you see?
Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
"Science is about questioning the status quo. Questioning authority".
In the absence of evidence, opinion is indistinguishable from prejudice.
|