I have just started learning about multithreading in C++... is there a difference between t1 and t2?
#include <iostream>
#include <thread>
#include <mutex>
std::mutex mutexCout;
//prints the value of x, and then increments it
//param: int value to display and increment
void foo (int& x)
{
std::lock_guard<std::mutex> guard_(mutexCout);
std::cout << "x is " << x << "\n";
++x;
}
//testing different ways to call a function with reference
int main()
{
int x = 5;
//is t1 different from t2???
std::thread t1 ([&] {foo(x)};
std::thread t2 (foo, std::ref(x));
{
std::lock_guard<std::mutex> guard_(mutexCout);
std::cout << "x is " << x << "\n";
}
return 0;
}
Aucun commentaire:
Enregistrer un commentaire