简体   繁体   English

C++ 多线程程序在 Windows 中工作正常,在 Linux 中抛出“资源暂时不可用”

[英]C++ multithreaded program work fine in windows, throws "resource temporarily unavailable" in Linux

Edit:编辑:

Based on Jonathan's comment, I tried to create a standalone program to reproduce this issue.根据 Jonathan 的评论,我尝试创建一个独立程序来重现此问题。 I was unable to recreate the issue.我无法重现该问题。

Then I switched to Jeremy's comment to make sure I really am calling this only once.然后我切换到杰里米的评论,以确保我真的只打电话一次。 Turns out, there was a bug in the upstream code which was calling this in a loop when a specific condition was met.事实证明,上游代码中有一个错误,当满足特定条件时,它会在循环中调用它。

My first instinct was to have a flag before the thread create to check if it has been already created for the given dayIndex.我的第一个直觉是在线程创建之前有一个标志,以检查它是否已经为给定的 dayIndex 创建。 Like喜欢

    if (threadSpawnedForEachDay[dayIndex]) {
        return;
    }

Which took care of the "resource temporarily unavailable", but the output was still buggy.它处理了“资源暂时不可用”,但输出仍然有问题。 It took me a full 2 days to debug because I could only reproduce the behavior in Linux release build after 30 minutes.我花了整整 2 天的时间来调试,因为我只能在 30 分钟后重现 Linux 版本构建中的行为。 ( it has recursive logic and the bug shows up something like 100 calls deep ). 它具有递归逻辑,并且错误显示类似于 100 次调用深度)。

My original post我的原帖

I have a simple 2 threaded logic implemented in C++我有一个用 C++ 实现的简单 2 线程逻辑

  • A thread which sleeps for a limited time, and then flips a switch一个线程在有限的时间内休眠,然后触发一个开关
  • A main thread that does everything.一个做所有事情的主线程。 It checks the switch periodically, and when it's flipped it knows to wrap up and exit gracefully它定期检查开关,当它被翻转时,它知道要优雅地结束并退出

The program runs fine in Windows, but in Linux (Ubuntu 20.04) it eventually throws a system error: "Resource temporarily unavailable".该程序在 Windows 中运行良好,但在 Linux (Ubuntu 20.04) 中最终会引发系统错误:“资源暂时不可用”。 I tried htop on Linux, and it appears that the program is creating threads uncontrollably.我在 Linux 上尝试了 htop,似乎该程序无法控制地创建线程。 What could be causing this?这可能是什么原因造成的?

Here's my header:这是我的标题:

    struct TimeHelper {
        std::atomic_bool *timerForEachDay;
        std::vector<std::chrono::seconds> startTimeVector;
        bool isTimeUp(int dayIndex);
        void setStartTime(int dayIndex);
        void timerThread(int dayIndex);
        TimeHelper();
        ~TimeHelper();
        void TimeHelperInitializer();
    };
    extern TimeHelper timer;

Here's the code: some values hard coded here for brevity - my actual code uses a configuration file这是代码:为简洁起见,此处硬编码了一些值-我的实际代码使用配置文件

TimeHelper timer;

TimeHelper::TimeHelper() {
    timerForEachDay = NULL;
}

TimeHelper::~TimeHelper() {
    delete[] timerForEachDay;
}

//Separate initializer because the constructor is run before main
//This is only called once
void TimeHelper::TimeHelperInitializer() {
    timerForEachDay= new std::atomic_bool[2];
    for (int i = 0; i < 2; i++) {
        timerForEachDay[i]=false;
    }
    setStartTime(0); //setStartTime for other days is called elsewhere. It is only called once for each dayIndex at an appropriate time.
    return;
}

bool TimeHelper::isTimeUp(int dayIndex) {
    if (timerForEachDay[dayIndex] == true) return true;
    return false;
}

void TimeHelper::timerThread(int dayIndex) {
    std::this_thread::sleep_for(std::chrono::seconds(20));
    timerForEachDay[dayIndex] = true;
}

void TimeHelper::setStartTime(int dayIndex) {
    std::thread th(&TimeHelper::timerThread, this, dayIndex);
    th.detach();
    return;
}

Turns out there was a bug in the upstream code which was making repeated calls to setStartTime.结果发现上游代码中有一个错误,它重复调用 setStartTime。

The reason why the issue showed up only in Linux is because it is a much faster OS.该问题仅出现在 Linux 中的原因是因为它是一个更快的操作系统。 Windows would probably have the same experience if I let the code run for a few days.如果我让代码运行几天,Windows 可能会有相同的体验。 ( in case anyone's curious, the program tries to find the "best fit" for a given amount of time but if not available then switches to look for "a good enough fit" ). 如果有人好奇,程序会尝试在给定的时间内找到“最合适的”,但如果不可用,则切换到“足够合适”的位置)。

Thank you to everyone who commented on my question.感谢所有对我的问题发表评论的人。 Your comments steered me in the right direction.你的评论把我引向了正确的方向。

暂无
暂无

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

相关问题 C ++中的多线程抛出线程构造函数失败:资源暂时不可用 - Multi-Threading in C++ throws thread constructor failed: Resource temporarily unavailable C ++的递归线程使资源暂时不可用 - recursive threading with C++ gives a Resource temporarily unavailable 多线程 C++ 程序在 Windows 中使用 30% CPU(使用 MinGW 编译),但在 Linux 中使用 100% - Multithreaded C++ program using 30% CPU in Windows (compiled with MinGW), but 100% in Linux C ++:Linux代码在Windows上不起作用-将数据传递给程序 - C++: Linux code does not work on Windows - piping data to program 在Windows上编译MinGW的C ++程序是否适用于Linux上的GNU编译器? - Will a C++ program that compiles for MinGW on Windows work for GNU compiler on Linux? C ++,Clang,system_error:线程构造函数失败:资源暂时不可用 - C++, Clang, system_error: thread constructor failed: Resource temporarily unavailable C++ 程序在在线编译器和 linux 操作系统中运行良好,但在 Windows 上运行良好 - A C++ Program which works fine in online compilers and linux operating systems, but not on Windows 这个简单的c ++程序是多线程的吗? - Is this simple c++ program multithreaded? C++ - 套接字编程 - 多线程程序抛出请求的致命程序退出 - C++ - Socket Programming - Multithreaded program throws Fatal program exit requested 在Windows和LINUX中创建程序库[C ++] - Creating program libraries in Windows and LINUX [C++]
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM