1.

Can A Lambda Closure Be Used To Create A C++11 Thread?

Answer»

Yes ! A lambda closure is nothing but a variable storing a lambda expression. You can STORE a lambda in a closure if you intend to reuse the lambda expression at more than one place in your code.

#include "stdafx.h"

#include <thread>

#include <iostream>

using namespace std;

int main()

{

// Define a lambda closure

AUTO LaunchMissileFunc = []() -> void { COUT << "Launching Cruiser MISSILE" << endl; };

thread t1(LaunchMissileFunc);

t1.join();

RETURN 0;

}

Yes ! A lambda closure is nothing but a variable storing a lambda expression. You can store a lambda in a closure if you intend to reuse the lambda expression at more than one place in your code.

#include "stdafx.h"

#include <thread>

#include <iostream>

using namespace std;

int main()

{

// Define a lambda closure

auto LaunchMissileFunc = []() -> void { cout << "Launching Cruiser Missile" << endl; };

thread t1(LaunchMissileFunc);

t1.join();

return 0;

}



Discussion

No Comment Found