Starting Threads with Lambdas[]
A Lambda is, as we’ve seen, just an object and, like other objects it may be copied, passed as a parameter, stored in a container, etc. The Lambda object has its own scope and lifetime which may, in some circumstances, be different to those objects it has ‘captured’. Programers need to take special care when capturing local objects by reference because a Lambda’s lifetime may exceed the lifetime of its capture list: It must be ensured that the object to which the reference points is still in scope when the Lambda is called. This is especially important in multi-threading programs.
So let us start a thread and pass it a Lambda object to execute:
#include <iostream>
#include <thread>
int main()
{
int id = 0; // Define an integer variable
// starting a first thread (by reference)
auto f0 = [&id]() {
std::this_thread::sleep_for(std::chrono::milliseconds(100));
std::cout << "a) ID in Thread (call-by-reference) = " << id << std::endl;
};
std::thread t1(f0);
// starting a second thread (by value)
std::thread t2([id]() mutable {
std::this_thread::sleep_for(std::chrono::milliseconds(50));
std::cout << "b) ID in Thread (call-by-value) = " << id << std::endl;
});
// increment and print id in main
++id;
std::cout << "c) ID in Main (call-by-value) = " << id << std::endl;
// wait for threads before returning
t1.join();
t2.join();
return 0;
}
The output of the program looks like this
c) ID in Main (call-by-value) = 1
b) ID in Thread (call-by-value) = 0
a) ID in Thread (call-by-reference) = 1
As you can see, the output in the main thread is generated first, at which point the variable ID has taken the value 1. Then, the call-by-value thread is executed with ID at a value of 0. Then, the call-by-reference thread is executed with ID at a value of 1. This illustrates the effect of passing a value by reference : when the data to which the reference refers changes before the thread is executed, those changes will be visible to the thread. We will see other examples of such behavior later in the course, as this is a primary source of concurrency bugs.
网友评论