Talk:Asynchronous semaphore

From Wikipedia, the free encyclopedia
Jump to: navigation, search
WikiProject Computing  
WikiProject icon This article is within the scope of WikiProject Computing, a collaborative effort to improve the coverage of computers, computing, and information technology on Wikipedia. If you would like to participate, please visit the project page, where you can join the discussion and see a list of open tasks.
 ???  This article has not yet received a rating on the project's quality scale.
 ???  This article has not yet received a rating on the project's importance scale.
 

Not relevance enough[edit]

I am getting the impression that this is just a meme from the Node.js community that is not used outside of it, probably because what is described is not a semaphore at all but a barrier. I'm open to be proven wrong, but if I'm correct I'd vote for removal of this article --LeonT85 (talk) 22:57, 18 February 2012 (UTC)


- This is not a meme, it's a term known for several years now. It has nothing to do with NodeJS specifically. Similar problems exist in UI coding and non-blocking I/O for tens of years now.

Not quite right[edit]

Somehow this seems kind of wrong. A semaphore should queue the invocations ob P and once the resource becomes available it will allow the task to continue. I don't see any queuing in this code. If i call P repeatedly the lock count simply decreases and nothing is invoked. — Preceding unsigned comment added by 212.200.237.158 (talk) 12:10, 11 May 2012 (UTC)

Suggestion: Let's fix the semaphore by using this code: Async Semaphore(JavaScript):

exports.asem = function(initLock){
        if(initLock)
                this.lock=initLock;
        else
                this.lock=0;
		this.queue = []
}
 
exports.asem.prototype.v = function(){
        this.lock++;
		if (this.lock > 0 && this.queue.length > 0)
		{
			// dequeue
			this.lock--;
			var next = this.queue.shift();
			if (next)
				next();
		}
}
 
exports.asem.prototype.p = function(next){
        if(this.lock > 0) {
			this.lock--;
			if (next)	
                next();
		} else {
			// enqueue
			this.queue.push(next);
		}
}

Javascript and atomar sections[edit]

For the code to work as expected, one has to assume an increment and a following if() statement (or the other way around) are an atomar operation despite multiple threads can exist in javascript (setTimeout, asynchronous requests etc.).

However, that is not guaranteed by javascript. How is an asynchronous semaphore supposed to work under multithreaded conditions? Is there any proof this would still work?

Considering this, another algorithm to guarantee a critical section (without use of semaphors, atomics or locks) is needed, for example Lamport's bakery algorithm.--192.33.88.200 (talk) 08:39, 28 March 2013 (UTC)