tags:

views:

632

answers:

2

Is it possible by any means to change the limit on the number of pthreads a process can create ? Currently on my linux system I can create around 380 threads but I want to increase that to say as long as memory is available.

+1  A: 

Look at this:

http://stackoverflow.com/questions/344203/maximum-number-of-threads-per-process-in-linux

And take a look at this as it might pertain to your question:

Serve one client with each server thread

skwllsp
`cat /proc/sys/kernel/threads-max` shows `16384`. But I cannot create more than 380 pthreads. I even tried putting a blank function as body of the threads.
Sukanto
what error do you get?
skwllsp
after 380 threads I get `EAGAIN` as return value of `pthread_create()`, which according to `pthread_create()` man page is "The system lacked the necessary resources to create another thread, or the system-imposed limit on the total number of threads in a process {PTHREAD_THREADS_MAX} would be exceeded."
Sukanto
Are you testing 32-bit application? If so have you checked that when you create a 381 thread you have enough space for stack of the new thread?
skwllsp
Or there might be a limit on virtual memory size for a process. You can see limits running `ulimit -a`
skwllsp
virtual memory size limit is `unlimited`
Sukanto
+2  A: 

Your problem is that you have not called pthread_detach on the threads in question. This tells pthread that the resources associated with each thread will be released when the thread terminates. You have to call either pthread_join or pthread_release on all threads to release thread resources. That means that you also have to call pthread_detach in your pthread_join cancellation handlers or leak.