ReadWriteLock
A read/write lock for node. Read/Write Locks are used to allow many actors to read from a resource, as long as nothing is writing to it. That also means that only one actor may be writing at any given time.
Install the ReadWriteLock module from 'npm install readwritelock', then accessing the ReadWriteLock module by:
var ReadWriteLock = ;
Creating a ReadWriteLock Lock:
var rwlock = ;
readLock(functions, [optional]timeout) Arguments
- functions - An function or a function array which have a callback as argument. These functions will begin to excute in parallel when the read lock is acquired. When the callback is called, it will release the read lock.
- timeout(millisecond) - An optional argument which specify the timeout of the lock. If a timeout is specified, The function will release the read lock when the timeout reached, regardless of whether the callback is called.
Example
rwlock; rwlock;
writeLock(functions, [optional]timeout) Arguments
- functions - An function or a function array which have a callback as argument. Because the write lock are mutually exclusive, only one write lock function can be called at the same time. These functions will begin to excute in parallel when the write lock is acquired. When the callback is called, it will release the write lock.
- timeout(millisecond) - An optional argument which specify the timeout of the lock. If a timeout is specified, The function will release the write lock when the timeout reached, regardless of whether the callback is called.
Example
rwlock; rwlock;
For example,
var rwlock = ; rwlock; rwlock; rwlock;
The result will be
Start read 1!Start read 2!The reading procedure 2 is finished!The reading procedure 1 is finished!Start write 1!The writing procedure 1 is finished!Start read 3!The reading procedure 3 is finished!