vibe.d beta banner
get vibe.d
0.10.0

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

Function scopedMutexLock

Performs RAII based locking/unlocking of a mutex.

ScopedMutexLock!M scopedMutexLock(M) (
  M mutex,
  LockMode mode = LockMode.lock
)
if (is(M : Mutex) || is(M : Lockable));

Note that while TaskMutex can be used with D's built-in synchronized statement, InterruptibleTaskMutex cannot. This function provides a library based alternative that is suitable for use with all mutex types.

Example

import vibe.core.core : runWorkerTaskH;

__gshared int counter;
__gshared InterruptibleTaskMutex mutex;

mutex = new InterruptibleTaskMutex;

Task[] tasks;

foreach (i; 0 .. 100) {
	tasks ~= runWorkerTaskH({
		auto l = scopedMutexLock(mutex);
		counter++;
	});
}

foreach (t; tasks) t.join();

assert(counter == 100);
Authors

Leonid Kramer, Sönke Ludwig, Manuel Frischknecht

Copyright

© 2012-2019 Sönke Ludwig

License

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