// future example
#include <future> // std::async, std::future
#include <cmath> // std::sqrt
#include <iostream> // std::cout
const int N = 1000;
int main ()
{
double *a = new double[N];
double *b = new double[N];
for (int i = 0; i < N; ++i)
{
a[i] = i;
}
std::future<void> *futures = new std::future<void>[N];
for (int i = 0; i < N; ++i)
{
futures[i] = std::async([&a, &b, i]
{
b[i] = std::sqrt(a[i]);
});
}
/*
for (int i = 0; i < N; ++i)
{
futures[i].get();
}*/
for (int i = 0; i < N; ++i)
{
std::cout << b[i] << std::endl;
}
return 0;
}
Ly8gZnV0dXJlIGV4YW1wbGUKI2luY2x1ZGUgPGZ1dHVyZT4gICAgICAgICAvLyBzdGQ6OmFzeW5jLCBzdGQ6OmZ1dHVyZQojaW5jbHVkZSA8Y21hdGg+ICAgICAgICAgIC8vIHN0ZDo6c3FydAojaW5jbHVkZSA8aW9zdHJlYW0+ICAgICAgIC8vIHN0ZDo6Y291dAoKY29uc3QgaW50IE4gPSAxMDAwOwoKaW50IG1haW4gKCkKewoJZG91YmxlICphID0gbmV3IGRvdWJsZVtOXTsKCWRvdWJsZSAqYiA9IG5ldyBkb3VibGVbTl07Cglmb3IgKGludCBpID0gMDsgaSA8IE47ICsraSkKCXsKCQlhW2ldID0gaTsKCX0KCQoJc3RkOjpmdXR1cmU8dm9pZD4gKmZ1dHVyZXMgPSBuZXcgc3RkOjpmdXR1cmU8dm9pZD5bTl07Cglmb3IgKGludCBpID0gMDsgaSA8IE47ICsraSkKCXsKCQlmdXR1cmVzW2ldID0gc3RkOjphc3luYyhbJmEsICZiLCBpXQoJCXsKCQkJYltpXSA9IHN0ZDo6c3FydChhW2ldKTsKCQl9KTsKCX0KCS8qCglmb3IgKGludCBpID0gMDsgaSA8IE47ICsraSkKCXsKCQlmdXR1cmVzW2ldLmdldCgpOwoJfSovCgkKCWZvciAoaW50IGkgPSAwOyBpIDwgTjsgKytpKQoJewoJCXN0ZDo6Y291dCA8PCBiW2ldIDw8IHN0ZDo6ZW5kbDsKCX0KCQoJcmV0dXJuIDA7Cn0=