VOOZH about

URL: https://www.geeksforgeeks.org/cpp/how-to-access-vectors-from-multiple-threads-safely-in-cpp/

⇱ How to Access Vectors from Multiple Threads Safely? - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

How to Access Vectors from Multiple Threads Safely?

Last Updated : 23 Jul, 2025

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


Output

Vector Elements:
10
20
Comment