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

25th Sep 2024, 11:47 AM
Ketan Lalcheta
Ketan Lalcheta - avatar
4 odpowiedzi
0
have you tried async?
26th Sep 2024, 12:56 AM
Bob_Li
Bob_Li - avatar
0
Yes , but not using async. Just a pure threading is the need https://sololearn.com/compiler-playground/cz46C4OrVwep/?ref=app
26th Sep 2024, 4:22 AM
Ketan Lalcheta
Ketan Lalcheta - avatar
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; }
26th Sep 2024, 5:03 AM
Bob_Li
Bob_Li - avatar
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?
26th Sep 2024, 8:18 AM
Ketan Lalcheta
Ketan Lalcheta - avatar