Object built-in object

ObjectLock

Lock is a built-in object that can be used to control concurrent access to fibers. You can acquire a lock through one fiber to prevent other fibers from acquiring it at the same time. Lock can passcoroutine.Lock() function creation

A common situation is that in a multi-threaded scenario, when multiple threads want to modify the same data, data inconsistency will occur. For example, if two threads want to modify the same value in the same data, the results may be inconsistent if the control is improper. At this time, using the Lock object can achieve mutually exclusive access to the same data.

The following is a simple example, using Lock to implement alternate execution of two fibers, and the value of the shared variable v is not 300.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
var coroutine = require("coroutine") var l = new coroutine.Lock() var v = 100 function f() { l.acquire() v = 200 coroutine.sleep(1) v = 300 l.release() } coroutine.start(f) coroutine.sleep(1) l.acquire() assert.notEqual(300, v) assert.equal(200, v) l.release()

First, a Lock object is created, enters the fiber f, acquires the lock, modifies the variable v, and then releases the lock. In the main thread, wait for the fiber f to complete... When the fiber f releases the Lock, the main thread starts to acquire the Lock and ensure that the value of the variable v is changed to 300.

inheritance relationship

%0 object object toString() toJSON() Lock Lock new Lock() acquire() release() count() object->Lock Condition Condition Lock->Condition Event Event Lock->Event Semaphore Semaphore Lock->Semaphore

Constructor

Lock

Constructor

1
new Lock();

member function

acquire

Get ownership of the lock

1
Boolean Lock.acquire(Boolean blocking = true);

Call parameters:

  • blocking: Boolean, specifies whether to wait, wait when true, default is true

Return results:

  • Boolean, returns whether the lock was successfully acquired, true indicates successful acquisition.

The acquire method is used to obtain ownership of the lock. When the lock is in the acquireable state, this method returns true immediately.

When the lock cannot be acquired and blocking is true, the current fiber goes to sleep. When other fibers release the lock, this method returns true.

When the lock cannot be acquired and blocking is false, the method returns false.


release

Release ownership of the lock

1
Lock.release();

This method will release ownership of the lock or throw an error if the current fiber does not own the lock.


count

Query the current number of waiting tasks

1
Integer Lock.count();

Return results:

  • Integer, returns the number of tasks

toString

Returns the string representation of the object. Generally, "[Native Object]" is returned. The object can be re-implemented according to its own characteristics.

1
String Lock.toString();

Return results:

  • String, returns the string representation of the object

toJSON

Returns a JSON format representation of the object, generally returning a collection of readable properties defined by the object.

1
Value Lock.toJSON(String key = "");

Call parameters:

  • key: String, not used

Return results:

  • Value, returns a value containing JSON serializable