简体   繁体   English

pthread_create 使用 ENOMEM 失败?

[英]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.在具有 4GB 实内存的 64 位 RHEL 机器上,我看到 pthread_create() 因 rc=12 (ENOMEM) 而失败。 The 'top' command shows the process is using 1G of virtual memory when thread creation fails.当线程创建失败时,'top' 命令显示进程正在使用 1G 的虚拟内存。

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).我能够创建 16 个可连接线程,但第 17 次和后续调用失败并显示 ENOMEM 错误(这显然意味着内存 - 或 - 某些其他资源不可用)。 Any thoughts on what's going wrong?关于出了什么问题的任何想法?

I ran the program under strace and saw the following:我在 strace 下运行程序并看到以下内容:

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()可能失败:没有足够的空间(没有“洞”足够大)所描述的分配线程的堆栈这里

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM