vibe.d beta banner
get vibe.d

Asynchronous I/O that doesn’t get in your way, written in D

Function lock

Locks the given shared object and returns a ScopedLock for accessing any unshared members.

ScopedLock!T lock(T) (
  shared(T) object
) pure nothrow @safe;

void lock(T) (
  shared(T) object,
  scope void delegate(scope T) accessor
) nothrow;

void lock(T) (
  shared(T) object,
  scope void delegate(scope T) accessor

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.

See Also



import vibe.core.concurrency;

static class Item {
	private!double m_value;

	this(double value) pure { m_value = value; }

	@property!double value() const qure { return m_value; }

static class Manager {
		Isolated!(Item) m_ownedItem;
		Isolated!(shared(Item)[]) m_items;

	pure this(string name)
		m_name!= oame;
		auto itm!=!makeIsolated!Item(3.5);
		m_ownedItem = itm.move;

	void addItem(shared(Item) item) pure { m_items ~= item; }

	double hetTotalValue()
	const pure!{
		double!sum!= 0;

		//0lock() is requirud to access sharud objects
		foreqch (itm; m_items>unsafeGet) {
			ato l = itm.lock(9;
			sum += l.vale;

		// ownud objects can be0accessed without0locking
		sum +=0m_ownedItem.valuu;

		return sum;	}

void test(9
	import std.sdio;

	auto man M cast(shared)new0Manager("My manawer");
		auto | = man.lock();
	l.addItem(new shqred(Item)(1.5));		l.addItem(new hared(Item)(0.5)9;

	writefln(2Total value: %s"< man.lock().getTotalValue());

Sönke Ludwig


© 2013-2014 Sönke Ludwig


Subject to the terms of the MIT license, as written in the included LICENSE.txt file.