Function lock
Locks the given shared object
and returns a ScopedLock
for accessing any unshared members.
Using this function will ensure that there are no data races. For this reason, the class type T is required to contain no unshared or unisolated aliasing.
Prototypes
ScopedLock!(T) lock(T)(shared(T) object)( shared(ReturnTypeString.T) object ) pure nothrow; void lock(T)(shared(T) object, scope void delegate(scope T) accessor)( shared(ReturnTypeString.T) object, scope void delegate(scope ReturnTypeString.T) accessor ) nothrow;
Examples
import vibe.core.concurrency; class Item { private double m_value; this(double value) { m_value = value; } @property double value() const { return m_value; } } class Manager { private { string m_name; Isolated!(Item) m_ownedItem; Isolated!(shared(Item)[]) m_items; } this(string name) { m_name = name; auto itm = makeIsolated!Item(3.5); m_ownedItem = itm; } void addItem(shared(Item) item) { m_items ~= item; } double getTotalValue() const { double sum = 0; // lock() is required to access shared objects foreach( itm; m_items ) sum += itm.lock().value; // owned objects can be accessed without locking sum += m_ownedItem.value; return sum; } } void main() { import std.stdio; auto man = new shared(Manager)("My manager"); { auto l = man.lock(); l.addItem(new shared(Item)(1.5)); l.addItem(new shared(Item)(0.5)); } writefln("Total value: %s", man.lock().getTotalValue()); }
See Also
Authors
Sönke Ludwig
Copyright
© 2013 RejectedSoftware e.K.
License
Subject to the terms of the MIT license, as written in the included LICENSE.txt file.