|
Boost Users : |
Subject: [Boost-users] [boost][shared_ptr] Trying to make a thread safe Copy-on-write class
From: Haledor wow (haledor_at_[hidden])
Date: 2009-09-28 11:59:47
Hi,
Im trying to make a thread safe Copy-on-write class, but i am unsure how.
I want a template class that stores a std::set<T> and i want to be able to
add/remove elements while using iterators at the same time without
conflicts. I would like to avoid making copies of the entire set each time i
want to iterate over the elements. I have tried to use shared_ptr.unique()
to decide when to create a new copy of the set but i have been unsuccessful
since shared_ptr.use_count() stays at the value 1. I am doing something
wrong.
I have tried to use something like this as my add function:
mutable boost::mutex _mutex;
typedef std::set<T> Set;
typedef boost::shared_ptr<Set> SetPtr;
void add(T element) {
boost::unique_lock<boost::mutex> lock(_mutex);
if (!(_mySet.unique() || _mySet == 0)) {
Set* tmp = _mySet.get();
_mySet = SetPtr(new Set(*tmp));
}
_mySet->insert(element);
}
My thought was that each time an iterator accesses the set the count would
increase. Could i make it so (should i)?
Any other ideas about a thread safe implementation of a set of elements?
I hope someone can help me.
Thanks.
Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net