std::thread 据说会导致无法使用的堆栈跟踪

std::thread supposedly leading to unusable stack trace

这个问题与所谓的 std::thread 不利有关。昨天随便翻了一下流行的开源分布式代理envoy by Lyft. When I was studying their threading portion i came across a comment which caught my eye. The comment说了下面的话:

Wrapper for a pthread thread. We don't use std::thread because it eats exceptions and leads to unusable stack traces

我不确定 吃掉异常和无法使用的堆栈跟踪是什么意思

任何人都可以解释它的含义以及为什么 std::thread 导致不可靠的堆栈跟踪吗?

大概他们有一些自定义的异常处理机制,用堆栈跟踪记录未捕获的异常。

std::thread 定义为捕获未处理的异常并调用 std::terminate:

if it terminates by throwing an exception, std::terminate is called

https://en.cppreference.com/w/cpp/thread/thread

C++ 标准对 std::threads 中的未捕获异常作了如下说明:

In [thread.thread.constr]:

... If the invocation of INVOKE(​DECAY_­COPY(​std​::​forward(f)), DECAY_­COPY(​std​::​forward(args))...) terminates with an uncaught exception, terminate shall be called.

据此,回答

Can anybody explain what it means and why std::thread results in a non reliable stack trace?

除非在线程 proc 本身内部以专用方式处理,否则线程只会在异常时终止,并且启动线程的代码堆栈不会对发生的事情有任何了解。