Object built-in object

Object Condition

Condition variable object

Condition variables are a mechanism that uses global variables shared between fibers to synchronize. It mainly includes two actions: 1) A thread waits for a certain condition to be established and suspends itself; 2) Another thread makes the condition to be established, And notify the waiting fiber to execute downward.

To prevent competition, each condition variable needs oneLockMatch (LockCan be explicitly created and passed in by yourself, or can be created by fibjs for you)

By using condition variables, one condition variable can be used to control the switch of a batch of fibers;

The following are two examples of fiber scheduling:

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
var coroutine = require("coroutine"); var cond = new coroutine.Condition(); var ready = false; var state = "ready"; function funcwait() { cond.acquire(); while (!ready) cond.wait(); state = "go" cond.release(); } coroutine.start(funcwait); cond.acquire(); console.log(state) ready = true; cond.notify(); coroutine.sleep(); console.log(state);

will output:

1 2
ready go

Inheritance

#lineWidth: 1.5 #font: Helvetica,sans-Serif #fontSize: 10 #leading: 1.6 #.this: fill=lightgray #.class: fill=white [<class>object|toString();toJSON()] [<class>Lock|new Lock()|acquire();release();count()] [<this>Condition|new Condition()|wait();notify();notifyAll()] [object] <:- [Lock] [Lock] <:- [Condition] Lock new Lock() acquire() release() count() Condition new Condition() wait() notify() notifyAll() object toString() toJSON()

Constructor

Condition

Condition variable constructor (the lock required by the condition variable is constructed internally by fibjs)

1
new Condition();

Condition variable constructor

1
new Condition(Lock lock);

Call parameters:

  • lock: Lock, Use a self-constructed lock

Member function

wait

Put fiber into blocking state

1
Condition.wait();

notify

Notify a blocked fiber (the one that was added to the fiber pool last) to continue execution downwards

1
Condition.notify();

notifyAll

Notify all blocked fibers to continue execution downward

1
Condition.notifyAll();

acquire

Acquire ownership of the lock

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

Call parameters:

  • blocking: Boolean, specify whether to wait, if it is true, wait, the default is true

Return result:

  • Boolean, Returns whether the lock is successfully acquired, true means the acquisition is successful

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

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

When the lock is not available and blocking is false, the method returns false.


release

Release lock ownership

1
Condition.release();

This method will release the ownership of the lock. If the current fiber does not own the lock, this method will throw an error.


count

Query the number of currently waiting tasks

1
Integer Condition.count();

Return result:

  • Integer, Returns the number of tasks

toString

Returns the string representation of the object, generally returns "[Native Object]", the object can be re-implemented according to its own characteristics

1
String Condition.toString();

Return result:

  • String, Returns the string representation of the object

toJSON

Returns the JSON format representation of the object, generally returns a collection of readable attributes defined by the object

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

Call parameters:

  • key: String, unused

Return result:

  • Value, Returns a value containing JSON serializable