I have some code on two systems running kernel 2.4.20 and kernel 2.4.38.
They both have gcc 3.2.2 and glibc 2.3.2
Under kernel 2.4.38, the pthread_t handles aren’t being reused. Under a heavy load test the application crashes once the handles reach 0xFFFFFFFF.
( I suspected this in the first place because the app crashes in deployments where IT uses a network port scanner- the threads are created for handling socket connections )
This simple example recreates the problem:
void* ThreadProc(void* param)
{
usleep(10000);
printf(" Thread 0x%x\n", (unsigned int)pthread_self());
usleep(10000);
return NULL;
}
int main(int argc, char* argv[])
{
pthread_t sThread;
while(1)
{
pthread_create(&sThread, NULL, ThreadProc, NULL);
printf("Created 0x%x\n", (unsigned int)sThread);
pthread_join(sThread, NULL);
};
return 0;
}
Under 2.4.20:
Created 0x40838cc0
Thread 0x40838cc0
Created 0x40838cc0
Thread 0x40838cc0
Created 0x40838cc0
Thread 0x40838cc0
...and on and on...
Under 2.4.36:
Created 0x4002
Thread 0x4002
Created 0x8002
Thread 0x8002
Created 0xc002
Thread 0xc002
...keeps growing...
How can I get kernel 2.4.36 to recycle handles? Unfortunately I can’t change kernel easily.
Thanks!
If your observations are correct, only two possible solutions exist.
Either
Option 2 is something you can do even if the kernel is misbehaving. You can hold a pool of threads that remain in a sleeping state when not being used. Thread pools are a widely known software engineering pattern (see http://en.wikipedia.org/wiki/Thread_pool_pattern). This is probably the better solution for you.