WilliamKF WilliamKF - 1 year ago 98
C++ Question

Reason for ENOMEM failure to create threads?

I have an application that is using pthread_create(), pthread_detach(), and pthread_exit().

After around 54 pthread_create() calls that have each been paired with a subsequent pthread_detach() and then pthread_exit() the pthread_create() fails. It is ENOMEM failure "Out of memory".

What might cause pthread_exit() to not be freeing up the memory of the old threads and causing my application to leak memory and eventually run out?

This is running on Linux Centos 5 64 bit but a 32 bit built application.

Answer Source

Will, I've deleted my old answer because it seems that I was mistaken. Along this line, I have a question: How did you get ENOMEM? Did you check "errno" as I had indicated in my answer? Because pthread_create is an exception to the rule, and does not set errno; instead, the error is returned as a result.

Correct way of getting failure reason:

int err = pthread_create(...);
    perror( "Error creating thread" );
    printf( "Error: %s\n", strerror( err ) );
    return false;

The reason I ask is because pthread_create will never fail with ENOMEM! In case of no memory, pthread_create will return EAGAIN. Refer to http://sourceware.org/ml/glibc-bugs/2007-11/msg00007.html for info on EAGAIN vs ENOMEM


Obvious question: the system does have enough free memory left, right?

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download