[an error occurred while processing this directive] pthread_create call returns error code 12 ENOMEM [an error occurred while processing this directive]

Problem: On Linux kernel 2.4 (RedHat) the pthread_create call returns error code 12 ENOMEM.

This undocumented behaviour occurs under Redhat Linux 2.4 when too many threads have been created in the non-detached mode, and the limited available memory in some system stack is consumed. At that point no new threads can be created in non-detached mode until those threads are detached/killed, or the parent process(es) killed and restarted.

Solution: create the threads in the detached state with the pthread_attr_setdetachstate call, e.g.;

pthread_attr_init (&id_attr);

pthread_attr_setdetachstate(&id_attr, PTHREAD_CREATE_DETACHED);

pthread_create( , &id_attr, , );

unless you really need the threads in the joinable (non-detached) state, in which case you will have a fixed upper limit to the number of joinable threads active at any one time. In the 2.4 kernels I've examined this limit appears to be 256 threads for each parent process(?).

Some of this is documented in the man help for pthread_attr_init. However the man help for pthread_create makes no mention of the ENOMEM return and what it means, which is rather confusing.

In the Linux 2.2 kernels I've examined the behaviour is slightly different. The system default for the maximum joinable threads alive at any one time seems to be 1024 (rather than 256), and pthread_create returns an error code 11 EAGAIN, and a global errno value of 4 ("Interrupted system call"). The solution is the same as for 2.4: if your process creates lots of threads, make them detached or manage carefully their maximum instantaneous population.

John Reynolds

December 2004 [an error occurred while processing this directive]