ObjectCondition
condition variable object
Condition variables are a mechanism that uses global variables shared between fibers for synchronization. It mainly includes two actions: 1) One thread waits for a certain condition to be true and suspends itself; 2) Another thread makes the condition true, And notify the waiting fiber to execute downward.
To prevent races, each condition variable needs aLockcooperation (LockYou can explicitly create it yourself and pass it in, or you can let fibjs create it 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
21var 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
2ready
go
inheritance relationship
Constructor
Condition
Condition variable constructor (the lock required for the condition variable is constructed internally by fibjs)
1new Condition();
condition variable constructor
1new Condition(Lock lock);
Call parameters:
- lock:Lock, using self-constructed locks
member function
wait
wait for a condition variable
1Boolean Condition.wait(Integer timeout = -1);
Call parameters:
- timeout: Integer, specifies the timeout in milliseconds. The default is -1, which means it will never timeout.
Return results:
- Boolean, returns true if the acquisition is successful, returns false if it times out.
notify
Notify a blocked fiber (the last one added to the fiber pool) to continue execution downwards
1Condition.notify();
notifyAll
Notify all blocked fibers to continue execution downwards
1Condition.notifyAll();
acquire
Get ownership of the lock
1Boolean Condition.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
1Condition.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
1Integer Condition.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.
1String Condition.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.
1Value Condition.toJSON(String key = "");
Call parameters:
- key: String, not used
Return results:
- Value, returns a value containing JSON serializable