[英]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 线程逻辑
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.