How to Access Vectors from Multiple Threads Safely? Last Updated : 28 Apr, 2025 Comments Improve Suggest changes Like Article Like Report In C++, a thread is a basic element of multithreading that represents the smallest sequence of instructions that can be executed independently by the CPU. In this article, we will discuss how to access a vector from multiple threads safely in C++. Safely Access Vectors from Multiple Threads in C++To access vectors from multiple threads safely, use a mutex to synchronize access to the vector. Acquire the mutex before performing any read or write operations and release it afterward. Ensure that all threads accessing the vector use the same mutex to prevent concurrent access. C++ Program to Access Vectors from Multiple Threads Safely C++ // C++ Program to illustrate how to access vectors from // multiple threads safely #include <iostream> #include <mutex> #include <thread> #include <vector> using namespace std; vector<int> myVector; // Mutex for safe access to the vector mutex mtx; // Function to add an element to the vector safely void addToVector(int value) { lock_guard<mutex> lock(mtx); // Acquire the mutex myVector.push_back(value); // Add element to the vector } int main() { // Create two threads to concurrently add elements to // the vector thread t1(addToVector, 10); thread t2(addToVector, 20); t1.join(); t2.join(); // Display the elements of the vector cout << "Vector elements: "; for (int value : myVector) { cout << value << " "; } cout << endl; return 0; } Output Vector Elements:1020 Comment More infoAdvertise with us Next Article How to Push All Elements from a Vector to a Queue in C++? S susobhanakhuli Follow Improve Article Tags : C++ Programs C++ STL cpp-vector cpp-multithreading CPP Examples +2 More Practice Tags : CPPSTL Similar Reads How to Access Vector Methods from Pointer to Vector in C++? In C++, we can create a pointer that points to the object of vector type. In this article, we will learn how to access member functions of std::vector from a pointer to the vector in C++. Example Input:vec = {1, 2, 3, 4}vector<int>* ptr = vecOutput:ptr->at(2): 3Accessing std::vector Methods 2 min read How to Push All Elements from a Vector to a Queue in C++? In C++, vectors are dynamic arrays while the queue is a data structure that follows the FIFO (First In First Out) property. In this article, we will learn how to push all elements from a vector to a queue in C++. Example Input: myVector = {1, 5, 8, 9, 4} Output: myQueue = 1 5 8 9 4Copy All Vector El 2 min read How to Capture std::vector in Lambda Function? In C++, lambda functions allow users to define an inline function anywhere in the program. They are also able to capture the objects from outside its definition. In this article, we will look at how to capture a std::vector object in lambda functions in C++. Capture std::vector in Lambda FunctionTo 2 min read Whatâs the Best Way to Map Multiple Vectors to Keys in a Multimap in C++? In C++, multimaps are associative containers in which multiple values can be stored corresponding to the same key and vectors are dynamic arrays that store data in contiguous memory locations. In this article, we will learn what's the best way to map multiple vectors to keys in a multimap in C++. Ex 2 min read How to Use the std::mutex Synchronization Primitive in C++ In multi-threaded programming, it is essential to ensure that shared resources are accessed in a controlled and synchronized manner to maintain data consistency and prevent race conditions. The std::mutex synchronization primitive was introduced in C++ 11 to allow threads to acquire exclusive owners 3 min read C++ Program to Show Thread Interface and Memory Consistency Errors C++ allows Multithreading by using the 'thread' header file. The program acts as one thread but to increase program execution time/performance we can use threads to run parts of the program concurrently. But it may lead to issues of memory consistency errors and may not give us the proper output. Th 2 min read Like