0
Best way to get return value from thread
Hi I have a function which returns a value. Does there a better way to return the value from thread compare to what I have here in below code? https://sololearn.com/compiler-playground/cFn1Tvsfe4dm/?ref=app
4 Réponses
0
have you tried async?
0
Yes , but not using async. Just a pure threading is the need
https://sololearn.com/compiler-playground/cz46C4OrVwep/?ref=app
0
//you can use ref
#include <iostream>
#include <thread>
using namespace std;
void getData(int& n){
n = 100;
}
int main() {
int retVal;
thread t(getData, ref(retVal));
t.join();
cout << retVal << endl;
}
0
Thats true but pass by ref is crude way. I will have to change the existing function signature also. Thanks 😊 but is there any other option?