Copy-on-write: Difference between revisions

Content deleted Content added
mNo edit summary
Undid revision 1089088590 by Comp.arch (talk): Pointless edit, and contrary to the norm.
Line 24:
COW is also used in [[Library (computer science)|library]], [[Application software|application]] and [[System software|system]] code.
 
In [[multithreadingMultithreading (computer architecture)|multithreaded]] systems, COW can be implemented without the use of traditional [[lockLock (computersoftware scienceengineering)|locking]] and instead use [[compare-and-swap]] to increment or decrement the internal reference counter. Since the original resource will never be altered, it can safely be copied by multiple threads (after the reference count was increased) without the need of performance-expensive locking such as [[lockLock (computer science)|mutexes]]. If the reference counter turns 0, then by definition only 1 thread was holding a reference so the resource can safely be de-allocated from memory, again without the use of performance-expensive locking mechanisms. The benefit of not having to copy the resource (and the resulting performance gain over traditional deep-copying) will therefore be valid in both single- and multithreaded systems.
 
===Examples===
The [[stringString (C++)|string]] class provided by the [[C++ standard library]] was specifically designed to allow copy-on-write implementations in the initial C++98 standard,<ref name="meyers">{{citation |first=Scott |last=Meyers |author-link=Scott Meyers |year=2012 |title=Effective STL |publisher=Addison-Wesley |pages=64–65 |url=https://books.google.com/books?id=U7lTySXdFk0C&pg=PT734|isbn=9780132979184 }}</ref> but not in the newer C++11 standard:<ref>{{cite web|title=Concurrency Modifications to Basic String|url=http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2008/n2534.html|website=Open Standards|access-date=13 February 2015}}</ref>
<syntaxhighlight lang="cpp">
std::string x("Hello");