This is the test program that I am working with. Can someone please describe in detail what is happening and the reason for such and output.
Why does launch::async get the g_num value as 0, while launch::deferred gets 100.
Both launch::async and launch::differed got correct values of arg that is on the main stack, which I believe means they should have both gotten 100.
#include <iostream>
#include <future>
using namespace std;
thread_local int g_num;
int read_it(int x) {
return g_num + x;
}
int main()
{
g_num = 100;
int arg;
arg = 1;
future<int> fut = async(launch::deferred, read_it, arg);
arg = 2;
future<int> fut2 = async(launch::async, read_it, arg);
cout << "Defer: " << fut.get() << endl;
cout << "Async: " << fut2.get() << endl;
return 0;
}
The output:
Defer: 101
Async: 2
Aucun commentaire:
Enregistrer un commentaire