A quiote from Nikolai Josuttis - Standard Library C++11:
Detached threads can easily become a problem if they use nonlocal resources. The problem is that you lose control of a detached thread and have no easy way to find out whether and how long it runs. Thus, make sure that a detached thread does not access any objects after their lifetime has ended. For this reason, passing variables and objects to a thread by reference is always a risk. Passing arguments by value is strongly recommended.
So further the autohor explains, that exen if you pass a reference as a function argument to a thread, it still passes by value, so you must indicate the reference with std::ref
.
This I have the questions, see the code below:
void f(std::vector<int> V){...}
void g(std::vector<int>& V){...}
std:vector<int> V;
std::thread t1(f, V);
std::thread t2(f, std::ref(V));
std::thread t3(g, V);
std::thread t4(g, std::ref(V));
Whats the difference in this 4 lines? Which lines are equivalent?
I am not joining or detaching thread, its not about that, its about the ways of passing the funtion argument.
Aucun commentaire:
Enregistrer un commentaire