Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Вивчайте Passing Arguments to Threads | Concurrency Foundations
C++ Concurrency and Multithreading

bookPassing Arguments to Threads

When working with threads in C++, you often need to pass data to the thread function. Understanding how arguments are passed—by value, by reference, or by move—is essential for writing safe and efficient concurrent code. The std::thread constructor copies its arguments by value by default. This means that if you pass a variable to a thread, the thread function receives a copy, not the original variable. If you want the thread to modify the original variable, you must explicitly pass it by reference. To do this, use std::ref to wrap the variable. If you have a resource that cannot or should not be copied, such as a unique pointer, you can transfer ownership to the thread by using std::move.

main.cpp

main.cpp

copy
1234567891011121314151617181920212223242526272829303132333435363738394041
#include <iostream> #include <thread> #include <string> #include <utility> void printByValue(std::string msg) { std::cout << "By value: " << msg << std::endl; msg = "Changed inside thread"; } void printByReference(std::string& msg) { std::cout << "By reference: " << msg << std::endl; msg = "Changed inside thread"; } void printByMove(std::string msg) { std::cout << "By move: " << msg << std::endl; msg = "Changed inside thread"; } int main() { std::string message1 = "Hello, World!"; std::string message2 = "Reference message"; std::string message3 = "Movable message"; std::thread t1(printByValue, message1); std::thread t2(printByReference, std::ref(message2)); std::thread t3(printByMove, std::move(message3)); t1.join(); t2.join(); t3.join(); std::cout << "After threads:\n"; std::cout << "message1: " << message1 << std::endl; std::cout << "message2: " << message2 << std::endl; std::cout << "message3: " << message3 << std::endl; return 0; }
Note
Passing References to Threads: Common Mistake

A common mistake when passing arguments to threads is forgetting to use std::ref when you want to pass by reference. If you pass a variable directly, the thread function receives a copy, not a reference. This can lead to unexpected behavior, especially if you expect the thread to modify the original variable. Always use std::ref if you need to pass a reference to a thread function.

question mark

Which method of passing arguments to a thread should you use if you want the thread to modify the original variable and see the changes after the thread completes?

Select the correct answer

Все було зрозуміло?

Як ми можемо покращити це?

Дякуємо за ваш відгук!

Секція 1. Розділ 5

Запитати АІ

expand

Запитати АІ

ChatGPT

Запитайте про що завгодно або спробуйте одне із запропонованих запитань, щоб почати наш чат

Suggested prompts:

Can you show an example of passing arguments by reference to a thread?

How do I use std::move with std::thread?

What happens if I forget to use std::ref when passing by reference?

bookPassing Arguments to Threads

Свайпніть щоб показати меню

When working with threads in C++, you often need to pass data to the thread function. Understanding how arguments are passed—by value, by reference, or by move—is essential for writing safe and efficient concurrent code. The std::thread constructor copies its arguments by value by default. This means that if you pass a variable to a thread, the thread function receives a copy, not the original variable. If you want the thread to modify the original variable, you must explicitly pass it by reference. To do this, use std::ref to wrap the variable. If you have a resource that cannot or should not be copied, such as a unique pointer, you can transfer ownership to the thread by using std::move.

main.cpp

main.cpp

copy
1234567891011121314151617181920212223242526272829303132333435363738394041
#include <iostream> #include <thread> #include <string> #include <utility> void printByValue(std::string msg) { std::cout << "By value: " << msg << std::endl; msg = "Changed inside thread"; } void printByReference(std::string& msg) { std::cout << "By reference: " << msg << std::endl; msg = "Changed inside thread"; } void printByMove(std::string msg) { std::cout << "By move: " << msg << std::endl; msg = "Changed inside thread"; } int main() { std::string message1 = "Hello, World!"; std::string message2 = "Reference message"; std::string message3 = "Movable message"; std::thread t1(printByValue, message1); std::thread t2(printByReference, std::ref(message2)); std::thread t3(printByMove, std::move(message3)); t1.join(); t2.join(); t3.join(); std::cout << "After threads:\n"; std::cout << "message1: " << message1 << std::endl; std::cout << "message2: " << message2 << std::endl; std::cout << "message3: " << message3 << std::endl; return 0; }
Note
Passing References to Threads: Common Mistake

A common mistake when passing arguments to threads is forgetting to use std::ref when you want to pass by reference. If you pass a variable directly, the thread function receives a copy, not a reference. This can lead to unexpected behavior, especially if you expect the thread to modify the original variable. Always use std::ref if you need to pass a reference to a thread function.

question mark

Which method of passing arguments to a thread should you use if you want the thread to modify the original variable and see the changes after the thread completes?

Select the correct answer

Все було зрозуміло?

Як ми можемо покращити це?

Дякуємо за ваш відгук!

Секція 1. Розділ 5
some-alt