C++C++11C++14C++17C++20Learn C++Syntax

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.

we can declare an integer vec vector as shown below.

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.

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.

Here we used myf() function. Note that you can use methods of class objects, functors, and lambda expressions too.

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

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.

Oh hi there 👋
It’s nice to meet you.

Sign up to receive awesome C++ content in your inbox, every day.

We don’t spam! Read our privacy policy for more info.

About author

Dr. Yilmaz Yoru has 35+ years of coding with more than 30+ programming languages, mostly C++ on Windows, Android, Mac-OS, iOS, Linux, and some other operating systems. He graduated and received his MSc and PhD degrees from the Department of Mechanical Engineering of Eskisehir Osmangazi University. He is the founder and CEO of ESENJA LLC Company. His interests are Programming, Thermodynamics, Fluid Mechanics, Artificial Intelligence, 2D & 3D Designs, and high-end innovations.
Related posts
C++C++11C++14C++17C++20Introduction to C++Learn C++Syntax

Learn Default Constructors Of Classes In Modern C++

Artificial Intelligence TechC++C++11C++14C++17Learn C++

How To Develop Special AI Activation Functions In C++?

Artificial Intelligence TechC++C++11C++14C++17Language FeatureLearn C++

Learn How To Read The WAV Waveform Audio File Format In C++

C++C++11Learn C++

What Is Assignment Operator Overloading?