In modern mathematics, physics, and computer science; optimized and faster app development in programming is very important to speed up computations. CPUs and GPUs are highly evolved by the number of cores and transistors to give more computational power to today’s servers and computers. Thus, we can use more cores and threads in our applications by using std::thread
. We can use the std::thread
class in multi-thread operations, and in this post, we explain how to use std::thread and how can we use it with modern C++ examples.
Artificial Intelligence, Image – Video processing, computational mechanics, fluid dynamics, etc. etc. requires using all cores in many threads. The std::thread
class is a special class defined in <thread>
header in the concurrency support library that comes with C++11.
Let’s see how it works with some very simple examples.
What is std::thread in modern 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. Here when we call the join method, the thread object becomes non-joinable
and can be destroyed safely, returns when the thread execution has completed.
Let’s see how we can use std::thread
in different function definitions.
Is there an example of running a function using std::thread in modern C++?
Here is a very simple example that runs myf()
function in one single thread myt
.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
#include <thread> void myf(int r) { double a = M_PI*r*r; } int main() { std::thread myt( myf, 5); // add myf(5) to thread myt.join(); // join thread to wait the thread execution } |
Is there an example about running class method with std::thread in modern C++?
Here is a std::thread
example that runs myf()
function in a class object o1
,
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
#include <thread> class Tx { public: void myf(int r) { double a = M_PI*r*r; } } int main() { Tx o1; std::thread myt(&Tx::myf, &o1, 5); // add myf(5) to thread myt.join(); // join thread to wait the thread execution } |
Here is another good example how you can use more threads with a thread vector.
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.