Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

std::thread supposedly leading to unusable stack trace

The question is related to alleged disadvantage of std::thread. Yesterday I was casually traversing the popular open source distributed proxy envoy by Lyft. When I was studying their threading portion i came across a comment which caught my eye. The comment says the following:

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

I am not sure what does it mean by eats exception and unusable stack traces.

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

like image 685
sujat Avatar asked Aug 16 '18 08:08

sujat


2 Answers

Presumably they have some custom exception handling mechanism which logs uncaught exceptions with a stack trace.

std::thread is defined to catch unhandled exceptions and call std::terminate:

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

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

like image 51
Alan Birtles Avatar answered Sep 23 '22 13:09

Alan Birtles


The C++ Standard says the following regarding uncaught exceptions in 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.

According to this, the answer to

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

is that unless handled in a dedicated fashion inside the thread proc itself, the thread will just terminate upon an exception, and the code stack that started the thread will not be any the wiser regarding what had transpired.

like image 42
Geezer Avatar answered Sep 23 '22 13:09

Geezer