Skip to content

Instantly share code, notes, and snippets.

@jhurliman
Forked from elarkin/Mutex.js
Created July 14, 2012 21:17
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jhurliman/3113407 to your computer and use it in GitHub Desktop.
Save jhurliman/3113407 to your computer and use it in GitHub Desktop.
A simple mutex library for node.js
var EventEmitter = require('events').EventEmitter;
module.exports = function() {
var queue = new EventEmitter();
var locked = false;
this.lock = function lock(fn) {
if (locked) {
queue.once('ready', function() { lock(fn); });
} else {
locked = true;
fn();
}
};
this.release = function release() {
locked = false;
queue.emit('ready');
};
};
@ORESoftware
Copy link

ORESoftware commented Dec 23, 2016

nice, will work for single process, but not multiple processes right? this.lock is global...?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment