如何以线程安全的方式在一个容器上迭代? [英] How to iterate over a container in a thread-safe way?
问题描述
我有一个容器(C ++),我需要在两种方式,从不同的线程操作:1)添加和删除元素,和2)迭代通过其成员。显然,在迭代发生时删除元素=灾难。代码如下:
I have a container (C++) on which I need to operate in two ways, from different threads: 1) Add and remove elements, and 2) iterate through its members. Clearly, remove element while iteration is happening = disaster. The code looks something like this:
class A
{
public:
...
void AddItem(const T& item, int index) { /*Put item into my_stuff at index*/ }
void RemoveItem(const T& item) { /*Take item out of m_stuff*/ }
const list<T>& MyStuff() { return my_stuff; } //*Hate* this, but see class C
private:
Mutex mutex; //Goes in the *Item methods, but is largely worthless in MyStuff()
list<T> my_stuff; //Just as well a vector or deque
};
extern A a; //defined in the .cpp file
class B
{
...
void SomeFunction() { ... a.RemoveItem(item); }
};
class C
{
...
void IterateOverStuff()
{
const list<T>& my_stuff(a.MyStuff());
for (list<T>::const_iterator it=my_stuff.begin(); it!=my_stuff.end(); ++it)
{
...
}
}
};
同样, B :: SomeFunction()
和 C :: IterateOverStuff()
正在异步调用。我可以使用什么数据结构来确保在迭代期间, my_stuff
是从加法或删除操作的protected?
Again, B::SomeFunction()
and C::IterateOverStuff()
are getting called asynchronously. What's a data structure I can use to ensure that during the iteration, my_stuff
is 'protected' from add or remove operations?
推荐答案
听起来像是读写器锁。基本上,这个想法是,你可能有一个或多个读者或一个作家。您不能同时拥有读写锁。
sounds like a reader/writer lock is needed. Basically, the idea is that you may have 1 or more readers OR a single writer. Never can you have a read and write lock at the same time.
编辑:我认为适合您设计的使用示例包括:一个小变化。将一个iterate函数添加到拥有列表并使其模板化的类中,以便可以传递函数/函子来定义每个节点要做什么。这样的东西(快速和脏的伪代码,但你得到的点......):
An example of usage which I think fits your design involves making a small change. Add an "iterate" function to the class which owns the list and make it templated so you can pass a function/functor to define what to do for each node. Something like this (quick and dirty pseudo code, but you get the point...):
class A {
public:
...
void AddItem(const T& item, int index) {
rwlock.lock_write();
// add the item
rwlock.unlock_write();
}
void RemoveItem(const T& item) {
rwlock.lock_write();
// remove the item
rwlock.unlock_write();
}
template <class P>
void iterate_list(P pred) {
rwlock.lock_read();
std::for_each(my_stuff.begin(), my_stuff.end(), pred);
rwlock.unlock_read();
}
private:
rwlock_t rwlock;
list<T> my_stuff; //Just as well a vector or deque
};
extern A a; //defined in the .cpp file
class B {
...
void SomeFunction() { ... a.RemoveItem(item); }
};
class C {
...
void read_node(const T &element) { ... }
void IterateOverStuff() {
a.iterate_list(boost::bind(&C::read_node, this));
}
};
另一个选项将使读写器锁可公开访问,让调用者负责正确使用锁。但是这更容易出错。
Another Option would be to make the reader/writer lock publicly accessible and have the caller responsible for correctly using the lock. But that's more error prone.
这篇关于如何以线程安全的方式在一个容器上迭代?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!