Lambda Expressions allow users to write an inline expression that can be used for short snippets of code that are not going to be reused and don’t require naming in your C++ app. The lambda expressions construct is introduced in C++ 11 and further developed in the C++17 and C++20 standards. In modern C++ development, they are very useful and can be used with std::thread
in multi-thread operations. The std::thread
class is a special class defined in <thread>
header in the concurrency support library that comes with C++11. We can use the std::thread
class in multi-thread operations with lambda expressions. In this post, we explain how to use a Lambda expression with std::thread
in C++ with examples.
First, let’s learn what is a lambda in C++.
What is a lambda expression in C++?
A Lambda Expression defines an anonymous function or a closure at the point where it is used. You can think of a lambda expression as an unnamed function (that’s why it’s called “anonymous”). Lambda expressions help make code cleaner, more concise, and allow you to see behavior inline where it’s defined instead of referring to an external method, like a function.
The syntax for a lambda expression consists of specific punctuation with = [ ]
( )
{ ... }
series.
Lambda expressions are one example of modern C++ language features. If you want to know more about lambda expressions, you can read more about them here.
What is std::thread in C++?
In modern C++, a concurrency support library is designed to solve problems in multi-thread operations. This library includes built-in support for threads (std::thread
), atomic operations (std::atomic
), mutual exclusion (std::mutex
), condition variables (std::condition_variable
), and many other features.
The std::thread
class is a special class defined in <thread>
header that comes with C++11, and it allows multiple functions to execute and represents a single thread of execution concurrently. We can use the join()
method of the thread that waits for the thread to finish its execution.
Let’s see how we can use std::thread
with a lambda expression,
Is there an example of using a lambda expression with std::thread in C++?
Here is a simple example how we can use lambda expression with std::thread in C++,
1 2 3 4 5 6 7 8 9 10 11 12 13 |
#include <thread> int main() { auto lambda = [](int r) { int a = r*r; }; // lambda definition std::thread myt(lambda, 5); // add myf(5) to thread myt.join(); // join thread to run in threads } |
C++ Builder is the easiest and fastest C and C++ IDE for building simple or professional applications on the Windows, MacOS, iOS & Android operating systems. It is also easy for beginners to learn with its wide range of samples, tutorials, help files, and LSP support for code. RAD Studio’s C++ Builder version comes with the award-winning VCL framework for high-performance native Windows apps and the powerful FireMonkey (FMX) framework for cross-platform UIs.
There is a free C++ Builder Community Edition for students, beginners, and startups; it can be downloaded from here. For professional developers, there are Professional, Architect, or Enterprise version.