VS CTP 14的std::线程析构函数崩溃
Crash on std::thread destructor with VS CTP 14
我正在使用Visual Studio CTP 14,基于C++11中的标准库,实现一个具有可中断线程的并发库,如boost和Java中的线程。
在一些重构之后,我在std::thread的析构函数中遇到了崩溃(有时试图取消引用空指针,有时是"Debug Error!R6010",有时根本没有崩溃)。经过大量的代码剥离以找到问题,我没有更深入地了解问题是与我的代码有关,还是可能是编译器错误。
即使在剥离代码后,仍有很多需要复制的问题,所以请耐心等待:)
线程包装器实现的组成部分:
class InterruptException : public std::exception
{
public:
const char* what() const override {return "InterruptException";}
};
class Thread
{
public:
Thread(const Thread&) = delete;
Thread& operator=(const Thread&) = delete;
Thread();
template <typename Callable>
Thread(Callable&& action);
Thread(Thread&& other);
~Thread();
Thread& operator=(Thread&& other);
// Sets interruption flag and call notifyAll for current condition if any.
void interrupt();
// Throws interrupted exception if current thread interruption flag is set.
// This also resets the interruption flag.
static void interruptionPoint();
static void interruptCurrent();
static void setCondition(std::condition_variable* cond);
static Thread* currentThread(Thread* setter = nullptr);
private:
std::atomic<bool> _interruptionFlag;
std::atomic<std::condition_variable*> _currentCondition;
std::thread _stdThread;
};
template <typename Callable>
Thread::Thread(Callable&& action)
: _stdThread(),
_interruptionFlag(false),
_currentCondition(nullptr)
{
_stdThread = std::thread(
[this, runner = std::move(action)]
{
currentThread(this);
try
{
runner();
}
catch (InterruptException&)
{
// Normal exit.
}
catch (...)
{
// Removed logging calls.
}
}
);
}
Thread::~Thread()
{
// Block at thread destruction, no detached thread support.
if (_stdThread.joinable())
{
interrupt();
_stdThread.join();
}
}
// (More code if requested.)
(请注意,为了清晰起见,上面的代码缺少部分)
我已经为Thread类编写了几个测试,它似乎如人们所期望的那样工作。下一部分是任务运行器的精简版本。
template <typename Runner>
class StrippedSystem
{
public:
template <typename... CtorArgs>
StrippedSystem(CtorArgs&&... args);
StrippedSystem(const StrippedSystem&) = delete;
StrippedSystem(StrippedSystem&&) = delete;
// ...
private:
template <typename... CtorArgs>
Thread createRunnerThread(CtorArgs&&... args);
std::mutex _mutex;
std::condition_variable _cond;
Thread _runner;
};
template <typename Runner>
template <typename... CtorArgs>
StrippedSystem<Runner>::StrippedSystem(CtorArgs&&... args)
{
_runner = createRunnerThread(std::forward<CtorArgs>(args)...);
}
template <typename Runner>
template <typename... CtorArgs>
Thread StrippedSystem<Runner>::createRunnerThread(CtorArgs&&... args)
{
auto runnerProc =
[&]
{
Thread::setCondition(&_cond);
try
{
std::unique_lock<std::mutex> lock(_mutex);
_cond.wait(
lock,
[&]
{
Thread::interruptionPoint();
return false;
}
);
}
catch (...)
{
Thread::setCondition(nullptr);
std::rethrow_exception(std::current_exception());
}
};
return Thread(runnerProc);
}
精简的StripedSystem的代码非常简单。它创建了一个等待直到中断的线程。当调用Thread的析构函数时,会设置中断标志并通知条件。然后线程运行到一个中断点(在_cond.wait lambda内),该中断点抛出一个在线程包装器中捕获的中断异常,线程正常退出。
现在是崩溃一切的代码:
struct ConstructedRunner
{
ConstructedRunner(int a, std::string b)
: i(a), j(b)
{
}
int i;
std::string j;
};
int main(int argc, char* argv[])
{
{
StrippedSystem<ConstructedRunner> testSys2(1, "foobar");
}
return 0;
}
如上所述,崩溃可能是"调试错误!已调用中止"、访问冲突(试图取消引用空指针),或者在某些情况下根本没有崩溃。
经过一些故障排除,我发现以下不会崩溃:
struct DummyRunner
{
};
int main(int argc, char* argv[])
{
{
StrippedSystem<DummyRunner> testSys1;
}
return 0;
}
经过更多的故障排除,我发现在系统构造函数中替换
_runner = createRunnerThread(std::forward<CtorArgs>(args)...);
带有
_runner = Thread(
[&]
{
Thread::setCondition(&_cond);
try
{
std::unique_lock<std::mutex> lock(_mutex);
_cond.wait(
lock,
[&]
{
Thread::interruptionPoint();
return false;
}
);
}
catch (...)
{
Thread::setCondition(nullptr);
std::rethrow_exception(std::current_exception());
}
}
);
还修复了崩溃即使未使用转发的参数
这对我来说毫无意义,因为运行的代码应该是相同的。这是编译器问题,还是我真的做错了什么,导致了一些奇怪的并发问题?
使用Visual Studio CTP 14 for Windows 7在调试模式下构建。
经过一些帮助,我解决了错误。
问题出在Thread
模板构造函数中的currentThread(this)
上。该函数将线程指针设置为线程本地静态变量,稍后读取该变量以获取中断标志和当前条件(如果有的话)。问题是this
指针引用了一个临时对象。这可以在_runner = Thread(...)
中看到,其中临时Thread
对象位于右侧,并且被设置为线程本地数据。线程移动后未更新数据。
在Thread
的析构函数中,interrupt
检查了线程本地标志,这就是析构函数崩溃的原因。
- CoInitialize()在单独的线程上崩溃而不返回
- 为什么我的多线程作业队列崩溃
- C/C++ Linux 上的多线程服务器/客户端崩溃
- ZeroMQ 在使用 std::thread 创建工作线程时崩溃
- 使用-static libstdc++时std::线程弱,因此在运行时会导致崩溃
- Qt-工作线程崩溃时将cv::Mat转换为QImage
- 在官方 ZeroMQ 多线程示例的修改版本中崩溃
- 在提升时不一致崩溃::线程::release_handle?
- C 多线程崩溃应用程序
- 在 Unity 中C++使用后台线程进行图像处理会导致崩溃
- C++线程的向量在连接时随机崩溃
- Malloc()/free()在Windows上的几个线程中崩溃
- 使用多线程崩溃
- C++多线程应用程序崩溃
- CUDA内核无缘无故地使用20K 线程崩溃
- Qopenglwidget将上下文移至另一个线程崩溃
- 使用QT和QOGL的多线程崩溃
- 复制提升::跨线程崩溃的异常
- 释放模式下的boost线程崩溃
- 退出时分离的线程崩溃