Passing Arguments to Threads

This lesson gives an overview of how to pass arguments to threads in C++ -- by copy and by reference.

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 have to be extremely careful about the lifetime of the arguments; not doing so may result in undefined behavior.

Copy or Reference

Let’s have a look at a small code snippet:

Get hands-on with 1200+ tech skills courses.