Site icon Learn C++

How To Use std::thread With std::vector In Multi-Thread C++ Operations

How To Use stdthread With stdvector In Multi Thread C++ Operations

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++.

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.

[crayon-67639057b195a133475839/]

we can declare an integer vec vector as shown below.

[crayon-67639057b1962052564367/]

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.

[crayon-67639057b1965498806893/]

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.

[crayon-67639057b1968268149296/]

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.

Exit mobile version