Search⌘ K

Passing Arguments to Threads

Explore how to pass arguments to threads in modern C++, focusing on the differences between passing by copy, move, or reference. Understand why using std::ref is essential for passing arguments by reference to threads, and how to avoid undefined behavior by managing argument lifetimes properly.

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 num​ber of arguments.

In the case where your thread gets its data by reference, you ...