In C++, vectors are a modern and very flexible form of array in modern C++. Maybe we can say they are like modern linked lists. We can use vectors with std::thread
classes for multi-thread operations. In this post, we have a very good example that shows how you can use std::thread
and std::vector
together.
First, let’s remind ourselves about vectors and threads in C++.
Table of Contents
What is std::vector in C++?
Vectors are dynamic arrays included in <vector>
library in modern C++. They can resize themselves automatically when a member of a vector is inserted or deleted. Vectors are the same as dynamic arrays and these dynamic arrays of vectors are handled automatically by the container. Vectors are the way of Modern C++; their members are placed in contiguous memory storage; thus, they can be resized, and can be accessed and traversed using iterators. A vector can be defined using this syntax.
1 2 3 |
std::vector<object type> variable_name; |
we can declare an integer vec
vector as shown below.
1 2 3 |
std::vector<int> vec; |
We can add new members to this vector by using push_back()
method. If you are new to vectors, we have many previous posts about using vectors in C++, please check here for more features of amazing vector types.
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.
How to use std::thread with std::vector in C++ multi-threading?
Simply, we can create a vector that has thread class types as shown below.
1 2 3 |
std::vector< std::thread> myt; // vector for threads |
Now, we can add functions to this thread vector by using push_back()
method and std::thread()
, and we can join to wait each of them in an iteration by using join()
method of std::thread
. Here, the join() method of the thread 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 this in a full example that shows how we can use std::thread
with std::vector
.
Is there a full example of how to use std::thread with std::vector in C++?
Here is a full std::thread
example that uses std::vector
runs myf() function in given number of MAXN
threads of myt
.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 |
#include <iostream> #include <vector> #include <thread> #define MAXN 16 double areas[MAXN]; void myf(int r) { areas[r] = M_PI*r*r; } int main() { std::vector< std::thread> myt; // vector for threads for (unsigned int i = 1; i < MAXN; i++) { myt.push_back( std::thread( &myf, i) ); // add functions to thread vector } for (auto& t : myt) { t.join(); // join each to wait the thread execution } for (unsigned int i = 0; i < MAXN; i++) { std::cout << areas[i] << ","; } system("pause"); return 0; } |
Here we used myf()
function. Note that you can use methods of class objects, functors, and lambda expressions too.
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.
Design. Code. Compile. Deploy.
Start Free Trial
Free C++Builder Community Edition