Search⌘ K

Passing Arguments to Threads

Understand how to safely pass arguments to threads in C++. Explore passing by copy, move, or reference using std::thread, and learn to use std::ref to manage reference lifetimes and avoid undefined behavior.

We'll cover the following...

A thread, like any arbitrary function, can get its arguments by copy, by move, or by reference. std::thread is a variadic template which means that it takes an arbitrary number of arguments.

In the case where our thread gets its data by reference, we have to be extremely careful about the lifetime of the arguments; not doing so may result in undefined behavior.

...