pthread_create失败,并显示EAGAIN [英] pthread_create fails with EAGAIN

查看:128
本文介绍了pthread_create失败,并显示EAGAIN的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

请在此处考虑此代码段,在这里我试图创建一堆线程,这些线程最终将处理模拟竞态条件的给定任务.

Consider this code snippet here, where I am trying to create a bunch of threads which end up processing a given task which simulates a race-condition.

const int thread_count = 128;
pthread_t threads[thread_count];

for (int n = 0; n != thread_count; ++n)
{
    ret = pthread_create(&threads[n], 0, test_thread_fun, &test_thread_args);
    if( ret != 0 )
    {
        fprintf( stdout, "Fail %d %d", ret, errno );
        exit(0);
     }
 }

一切正常,除非pthread_create偶尔因errno EAGAIN资源暂时不可用"而失败,我尝试诱导usleep,然后重试创建,但没有实际效果.

Things generally works fine except occasionally pthread_create fails with errno EAGAIN "resource temporarily unavailable", I tried inducing usleep, and retry creating but with no real effect.

该故障是偶发性的,在某些情况下没有故障,并且在某些情况下非常频繁地发生.

the failure is sporadic and on some boxes no failures and on some occurs very frequently.

任何主意这里可能出什么问题吗?

any Idea what could be going wrong here ?

编辑-1

更新最大线程数

cat /proc/sys/kernel/threads-max
256467

编辑2

我认为这里的输入使我一直在思考,我可能会做下面的事情,并发布任何值得分享的结果.

I think the inputs here kept me thinking, i'll probably do the below and post any results that are worth sharing.

  1. 将堆栈大小设置为最小值,我不认为thread_function使用任何大数组.
  2. 增加我的记忆力和交换(并消除所有副作用)
  3. 编写脚本来监视系统行为,并在运行此案例时查看是否有其他进程/系统守护进程在干扰,这又可能导致资源紧张.
  4. 系统的硬限制和软限制都很高,因此在此我将其保留不变.

推荐答案

如果您的程序确保它所创建的线程数量绝不超过系统限制所允许的数量(通过在创建新线程之前加入线程),那么您很可能会遇到此内核错误:

If your program makes sure that it never creates more threads than the system limit allows for (by joining threads before creating new ones), then you are likely running into this kernel bug:

使用某些类型的容器技术,竞赛窗口似乎更大,并且更容易触发该错误.(这可能取决于所使用的cgroup的类型.)

With certain kinds of container technology, the race window appears to be much larger, and the bug is much easier to trigger. (This might depend on the types of cgroups in use.)

这篇关于pthread_create失败,并显示EAGAIN的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆