3

I'm developing an application which handles client connections. I'm spawning a thread for each request, since there will be short tasks. However, I keep having a problem after handling a certain amount of connections. Specifically, after 381 connections, pthread_create fails to create a new thread. I know this can fail if the application runs out of resources, or more than PTHREAD_THREADS_MAX threads have been already created.

The strange thing is the first 381 threads have already stopped when this error occurs. I'm not using pthread_join to wait for these threads to stop, I believe pthreads don't require me to somehow "stop" the thread, correct me if i'm wrong(at least the manpage does not mention this). I thought maybe this could be produced when several threads were spawn at the same time, however, I've tested it several times and every time the 382th thread creation fails.

Does anyone know what could be happening? Any help will be appreciated.

Thanks in advance.

mfontanini
  • 21,410
  • 4
  • 65
  • 73

1 Answers1

4

If you do not call pthread_join or detach the thread (either by calling pthread_detach or creating it in the detached state using attributes), the resources used by the terminated thread will never be freed. This is your problem. If you don't need to join your threads, detach them as soon as you create them.

R.. GitHub STOP HELPING ICE
  • 208,859
  • 35
  • 376
  • 711
  • Ok, i've reached 450+ pthread spawns, so i guess this was the problem. Thanks a lot! – mfontanini Apr 30 '11 at 20:41
  • 3
    Note that, by default, glibc/NPTL uses *insanely* large stack sizes for each thread (typically 8-10MB), which is why you ran out of virtual address space (32-bit) after 381 threads. Assuming you don't need so much, creating an attribute object and using `pthread_attr_setstacksize` to choose a much smaller stack size (64k should be enough for most uses) will save a lot of resources and reduce the risk of `pthread_create` failing if many threads are still running. Then you can also pre-set the detached attribute so you don't have to call `pthread_detach`. – R.. GitHub STOP HELPING ICE Apr 30 '11 at 20:48
  • nice one, i thought threads default stack size was much smaller. I'll take that advice as well! Thanks! – mfontanini Apr 30 '11 at 21:10