Passing 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
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; }
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.
Grazie per i tuoi commenti!
Chieda ad AI
Chieda ad AI
Chieda pure quello che desidera o provi una delle domande suggerite per iniziare la nostra conversazione
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?
Awesome!
Completion rate improved to 7.69
Passing Arguments to Threads
Scorri per mostrare il menu
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
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; }
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.
Grazie per i tuoi commenti!