fcf.NLock.unlockNamedMutex() function
[SERVER ONLY]
fcf.NLock.unlockNamedMutex (number a_lock, function a_cb)
Package: fcf-framework-lock
File: fcf-framework-lock:index.js
Available from version: 1.0.6
Performs a mutex unlock
Arguments
- The lock ID obtained by the fcf.NLock.lockNamedMutex or fcf.NLock.tryLockNamedMutex functions
- The function of processing the result of the function execution.
Function signature a_cb: a_cb(
-
Error |undefined a_error - Error object.
Example: Function application
Below is an example of implementing a critical section using named mutexes. The output to the terminal is made for two instance of the program running simultaneously.
let libLock = require("fcf-framework-lock");
async function main(){
const mutexName = "TestMutexForExample"
console.log(`PID:${process.pid}: ${new Date().toISOString()}: "Wait locking ..."`);
let lock = await new Promise((a_res, a_rej)=>{
libLock.lockNamedMutex(mutexName, (a_error, a_lock)=>{
if (!a_error){
a_res(a_lock);
} else {
a_rej(a_error);
}
});
});
console.log(`PID:${process.pid}: ${new Date().toISOString()}: Start of critical section`);
await new Promise((a_res, a_rej)=>{
setTimeout(()=>{
a_res(0);
}, 1000);
});
console.log(`PID:${process.pid}: ${new Date().toISOString()}: End of critical section`);
await new Promise((a_res, a_rej)=>{
libLock.unlockNamedMutex(lock, (a_error)=>{
if (!a_error){
a_res();
} else {
a_rej(a_error);
}
});
});
console.log(`PID:${process.pid}: ${new Date().toISOString()}: Process is exit`);
}
main();
Output:
PID:11058: 2023-10-21T19:05:18.221Z: Wait locking ...
PID:11059: 2023-10-21T19:05:18.222Z: Wait locking ...
PID:11058: 2023-10-21T19:05:18.226Z: Start of critical section
PID:11059: 2023-10-21T19:05:18.227Z: Failed to lock the mutex because the mutex is already locked
PID:11059: 2023-10-21T19:05:18.228Z: Error: Resource temporarily unavailable
PID:11059: 2023-10-21T19:05:18.228Z: Process is exit
PID:11058: 2023-10-21T19:05:19.228Z: End of critical section
PID:11058: 2023-10-21T19:05:19.228Z: Release mutex
PID:11058: 2023-10-21T19:05:19.229Z: Process is exit