何时在迭代中使用互斥锁。 c++

发布于 2024-10-07 19:53:57 字数 238 浏览 5 评论 0原文

C++ 提供了非常方便的方法来通过迭代来遍历容器,

for(vector<int>::const_iterator p = c.begin(); p!=c.end();p++){}

但是如果在进行此操作时,有人c.push_back(10)

它会打破循环吗?

每次使用容器操作时都应该使用互斥锁吗?

谢谢。

C++ provides very convinient way to traverse the container with iteration,

for(vector<int>::const_iterator p = c.begin(); p!=c.end();p++){}

but if when this is undergoing, someone c.push_back(10).

Will it break the loop?

Should I use mutex every time I use container operation?

Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

听,心雨的声音 2024-10-14 19:53:57

是的,这里有一个竞争条件。

如果您要从多个线程使用 STL 容器,并且这些线程中至少有一个可能会修改该容器,则需要同步对该容器的访问。

最简单的方法是使用互斥锁。

另一种选择是找到或实现一个提供您所需功能的线程安全容器。

Yes, there is a race condition here.

If you are going to be using an STL container from multiple threads and at least one of those threads may modify the container, you need to synchronize access to the container.

The easiest way to go about doing this is to use a mutex.

Another option would be to find or implement a thread-safe container that provides the capabilities you need.

如此安好 2024-10-14 19:53:57

是的,当可以从另一个线程进行写入时,您应该这样做。

http://www.sgi.com/tech/stl/thread_safety.html

如果多个线程访问单个容器,并且至少有一个线程可能进行写入,则用户有责任确保容器访问期间线程之间的互斥。

Yes, you should, when writes are possible from another threads.

http://www.sgi.com/tech/stl/thread_safety.html

If multiple threads access a single container, and at least one thread may potentially write, then the user is responsible for ensuring mutual exclusion between the threads during the container accesses.

分开我的手 2024-10-14 19:53:57

你有两个问题。首先,您遇到迭代器失效问题。由于push_back增加了向量的大小,因此它有可能调用分配。每当向量的容量(不是元素数量)发生变化时,向量中的所有迭代器都会失效。这是最容易处理的方法,方法是确保在开始循环之前调用 reserve(),以确保向量中有足够的空间来满足所有未来的 push_back 尝试,同时循环运行。

另一个问题,正如其他帖子中提到的,当插入可能与正在访问的元素同时运行时,您会遇到竞争条件。

You have two issues. First, you have an iterator invalidation issue. Because push_back increases the size of the vector, it has the possibility of invoking an allocation. whenever the capacity (not number of elements) of a vector changes, all iterators into the vector are invalidated. This is easiest to deal with by making sure to call reserve() before beginning the loop to make sure there is enough space in the vector to satisfy all future push_back attempts while the loop runs.

The other issue, as mentioned in the other posts, you have a race condition when an insert may be running concurrently to the element being accessed.

四叶草在未来唯美盛开 2024-10-14 19:53:57

是的,您需要使用互斥锁来保护整个向量。这是不使用向量在线程之间共享数据的一个很好的理由。大多数时候我们使用队列,用锁保护push/pop。

如果您需要使用大缓冲区共享数据,我建议使用某种带有“页面”的自定义结构,因此,您可以只阻塞一小部分,并让其他线程同时在内存的不同位置运行。

yes, you need to protect the whole vector with a mutex. And that is a good reason to dont use vectors to share data between threads. Most of the time we use queues, protecting push/pop with locks.

If you need to share data using big buffers, I would recommend using some kind of custom structure with "pages", so, you can block only a small part, and letting other threads run on different places of the memory at the same time.

娇柔作态 2024-10-14 19:53:57

最简单的方法是在迭代前加锁,完成

  1. 互斥锁
  2. 后解锁迭代
  3. 互斥锁解锁

The easiest way is to lock before iteration and unlock after finishing

  1. mutex lock
  2. Iteration
  3. mutex unlock
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文