Lock

package sys.thread

Available on cs, neko, cpp, macro, java, hl

A Lock allows blocking execution until it has been unlocked. It keeps track of how often release has been called, and blocks exactly as many wait calls.

The order of the release and wait calls is irrelevant. That is, a Lock can be released before anyone waits for it. In that case, the wait call will execute immediately.

Usage example:

var lock = new Lock();
var elements = [1, 2, 3];
for (element in elements) {
	// Create one thread per element
	new Thread(function() {
		trace(element);
		Sys.sleep(1);
		// Release once per thread = 3 times
		lock.release();
	});
}
for (_ in elements) {
	// Wait 3 times
	lock.wait();
}
trace("All threads finished");

Constructor

new()

Creates a new Lock which is initially locked.

Methods

release():Void

Releases the lock once.

The thread does not need to own the lock in order to release it. Each call to release allows exactly one call to wait to execute.

wait(?timeout:Float):Bool

Waits for the lock to be released, or timeout (in seconds) to expire. Returns true if the lock is released and false if a time-out occurs.

© 2005–2020 Haxe Foundation
Licensed under a MIT license.
https://api.haxe.org/sys/thread/Lock.html