我在代码中使用了std::thread
,并且创建了一些线程join
,然后重复它,直到完成该过程。但是,内存使用量随着时间的推移而增加并崩溃。起初我认为这是因为指针所以我将所有thread*
更改为thread
,但它仍然不起作用。我使用Visual Studio 2015,所以我也尝试使用gcc编写Code :: Blocks,但我也可以看到'抛出'std::system_error
''实例后被叫终止的消息。以下代码是测试的简化版本。
#include <thread>
void foo(){}
int main()
{
for(; ;)
{
std::thread th1(foo);
std::thread th2(foo);
std::thread th3(foo);
std::thread th4(foo);
std::thread th5(foo);
th1.join();
th2.join();
th3.join();
th4.join();
th5.join();
}
}
我使用了5 thread
s因为如果我只使用一个thread
,它看起来就像刚刚停止而没有错误。我使用Visual Studio 2015调试工具和Windows任务管理器检查了内存增量。它看起来thread
的析构函数不起作用并抛出std::system_error
因为没有更多的空间,但我无法理解为什么因为我调用了join()
所以应该删除thread
s对于每个循环。
是否存在重复构造和破坏thread
或std::thread
上的某些错误的问题?