Pthread_create fails after creating several threads
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.
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.
精彩评论