liveBook · Manning

Lambda

As explained in C++ Concurrency in Action, Second Edition

A lambda expression is a C++ expression that defines an unnamed callable, optionally capturing variables from its surrounding scope.

A lambda expression defines an unnamed callable inline. It can capture variables from its surrounding scope, avoiding a separately named function object or extra arguments. section 2.1.1 section 4.1.1

std::thread my_thread([]{
    do_something();
    do_something_else();
});

This lambda supplies the work for a new thread without defining a separate callable type. section 2.1.1

A lambda as a wait predicate

The condition-variable wait uses []{return !data_queue.empty();} to check whether work is available. The empty capture list is sufficient because the lambda reads no surrounding variables. section 4.1.1

Lambdas also handle typed messages: a handler can capture state by reference, update it, and choose the next state in a message-driven ATM. section 4.4.2

How C++ Concurrency in Action, Second Edition covers it

How deep it goes
Defines lambdas and illustrates practical uses, while deferring full expression details.
Examples
Shows thread work, a queue-nonempty wait predicate, and message handlers that update ATM state.
What you can do after
Apply lambda syntax to launch thread work, supply condition-variable predicates, and handle typed messages.
How it connects
Connects lambda expressions to threads, condition variables, predicates, message passing, and state machines.

Also explained in

Compare how 18 books explain lambda →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of C++ Concurrency in Action, Second Edition. The section links point to the parts of the book it draws on.