سلام.امروز من داشتم با thread کار میکردم به یک مشکل اساسی برخوردم یک متغیر را به صورت reference به thread پاس دادم و داخل thread تغییرش میدم ولی در main هیچ تغییری دیده نمیشه .اشکال از کجاست ؟
#include <iostream>;
#include <chrono>;
#include <thread>;
using namespace std;
void test(int& counter){
std::this_thread::sleep_for(chrono::milliseconds(500));
counter++;
cout << "thread counter: " << counter << endl;
}
int main(){
cout << "start" << endl;
int counter = 0;
std::thread a(test, counter);
a.join();
cout << "main thread counter:" << counter << endl;
return 0;
}