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.

In order to prevent competition, each condition variable needs a Lock ( Lock can be explicitly created and passed in by itself, or it can be created by fibjs for you)

By using condition variables, one condition variable can be used to control the switching 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

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 joined the fiber pool last) to continue execution downward

1
Condition.notify();

notifyAll

Notify all blocked fibers to continue execution downward

1
Condition.notifyAll();

acquire

Acquire lock ownership

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

Call parameters:

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

Return result:

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

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 current number of 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