Reputation: 26
I am seeing pthread_create() fail with rc=12 (ENOMEM), on a 64-bit RHEL machine with 4GB of real memory. The 'top' command shows the process is using 1G of virtual memory when thread creation fails.
I am able to create 16 joinable threads, but the 17th and subsequent calls fail with the ENOMEM error (which apparently means memory -or- some other resource is unavailable). Any thoughts on what's going wrong?
Upvotes: 0
Views: 2407
Reputation: 5211
pthread_create() can fail if the available virtual memory in the process is fragmented: there is not enough space (no "hole" sufficiently large) to allocate the thread's stack as described here.
Upvotes: 0
Reputation: 11
I ran the program under strace and saw the following:
mmap(NULL, 10489856, PROT_READ|PROT_WRITE|PROT_EXEC, MAP_PRIVATE|MAP_ANONYMOUS|0x40, -1, 0) = -1 ENOMEM (Cannot allocate memory) mmap(NULL, 10489856, PROT_READ|PROT_WRITE|PROT_EXEC, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0) = -1 ENOMEM (Cannot allocate memory)
UPDATE: Don't ask me why, but the following change fixes the issue:
pthread_attr_setscope(pattr, PTHREAD_SCOPE_SYSTEM);
Upvotes: 1