pthread_create fails w/ ENOMEM?
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 t开发者_开发问答he 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?
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);
I've found this. Seems to be a system limitation, but I'm not completely sure. However, that site provides a workaround.
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.
精彩评论