(Not) using std::thread
This post is about
std::thread
but not about threads or multi-threading. This is not an introduction to C++ threads. I assume that you are already familiar with Standard Library componentsthread
andasync
.I encountered a couple of introductions to C++ threads that gave a code example similar to the one below:
void run_in_parallel(function<void()> f1, function<void()> f2) { thread thr{f1}; // run f1 in a new thread f2(); // run f2 in this thread thr.join(); // wait until f1 is done }While it does give you a feel of what
thread
’s constructor does and how forking and joining works, I feel it does not stress enough how exception-unsafe this code is, and how unsafe using nakedthread
s in general is. In this post I try to analyze this “safety” issues...
Add a Comment
Comments are closed.