I'm trying to change a vector in a different thread, but the value of the vector is not changed. I thought that using std::ref
will fix the issue but it didn't work.
This is the code that start the threads:
printf("tmp size: %d\n", tmp_size);
printf("before change");
printArray(tmp);
std::thread threads[1];
for(int i = 0; i < 1; i++){
threads[i] = std::thread(callback, std::ref(tmp));
}
for(int i = 0; i < 1; i++){
threads[i].join();
}
printf("after join: ");
printArray(tmp);
this is the callback:
void callback(std::vector<uint64_t> tmp){
tmp[0] = 1;
printf("inside callback");
printArray(tmp);
}
and the output is:
tmp size: 2 before change 0 0 inside callback 1 0 after join: 0 0
I was expecting that after the thread change the vector the values will be: inside callback: 1 0. Isn't it passed by reference?