(Not) Using std::thread -- Andrzej Krzemieński

Andrzej Krzemieński writes:

(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 components thread and async.

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 naked threads in general is. In this post I try to analyze this “safety” issues...

Continue reading...

Add a Comment

Comments are closed.

Comments (0)

There are currently no comments on this entry.