I am using promises in C++, and have this really simple program. I create a promise, and pass it to a thread function by reference.
Inside the thread, I create a string and then set the promise's value to that string. Then in the main program, I retrieve the promise's value using a future. I'm confused about why the address of the string doesn't remain the same? Ideally it shouldn't vary between the threads since it belongs to shared memory right?
void tfunc(promise<string> & prms) {
string str("Hello from thread");
cout << (void *)str.data() << endl;
prms.set_value(str);
}
int main() {
promise<string> prms;
future<string> ftr = prms.get_future();
thread th(&tfunc, ref(prms));
string str = ftr.get();
// This address should be the same as that inside the thread, right?
cout << (void *)str.data() << endl;
th.join();
return 0;
}
Aucun commentaire:
Enregistrer un commentaire