A pit stepped into this afternoon

First let’s take a look at the following piece of code.

std::future<void> * ret_vals = new std::future<void>[3];
for (int i = 0; i < 3; ++i) {
  ret_val[i] = std::async([&]() {cout << "I am feeling great! Getting task " << i << endl});
}

Very trial use of async tasks and lambda in C++11 huh? I naively thought the code will produce some output like

I am feeling great! Getting task 0
I am feeling great! Getting task 1
I am feeling great! Getting task 2

But what it actually outputs is

I am feeling great! Getting task 3
I am feeling great! Getting task 3
I am feeling great! Getting task 3

It turns out that i is a variable captured by reference, and before the three async tasks have a chance to print it out, its value has long undergone many changes.

Leave a Reply

Your email address will not be published. Required fields are marked *