Re: Simple copy-on-write framework
Joe Seigh wrote:
Ulrich Eckhardt wrote:
I recently stumbled over the fact that using existing tools
it is quite easy to create a COW framework (almost) without
any additional locking for multithreaded code.
The idea is simple:
1. While you modify an object, you store it in a std::auto_ptr<T>. This
makes sharing the object impossible (due to auto_ptr's move semantics).
2. If you want to share an object, you store it in a boost::shared_ptr<T
[...]
Quite simple and charming, so I thought I'd share it. And also of course to
get some peer review.
You probably get not a few comments to check out String implementations that
use COW with reference counting.
Some comments. You'll probably want to make the shared_ptr an internal
opaque handle in your object class. Secondly, you will want to hold
the lock the whole time while you're modifying the object since the
modification of most datatypes is determined by the previous state of
the object.
Conceptually, he's modifying a snapshot made at a specific
moment. This is a standard data base technique, where it is
known as optimisitic locking. (In a data base, of course,
several users might try to modify the object at the same time.
So you also associate a version with the copied object, and if
the version has changed when you try to reinsert it into the
data base, the transaction fails. In a multithreaded
environment, you probably don't need this, since the two threads
are modifying what is conceptually two different objects.)
--
James Kanze (Gabi Software) email: james.kanze@gmail.com
Conseils en informatique orient?e objet/
Beratung in objektorientierter Datenverarbeitung
9 place S?mard, 78210 St.-Cyr-l'?cole, France, +33 (0)1 30 23 00 34
--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]