4 ms·
It can just be a boolean variable called locked?
by HeyImAlex 9y ago
It can just be a boolean variable called locked?
- deleted 9y ago[deleted]
- deleted 9y ago[deleted]
- nitely 9y agoAlmost. In threaded programming a thread will wait/block until the lock is released and then make use of the protected resource. In async world you would probably just check some flag and if it's "locked" then register a callback to some "unlocked" event to mimic this.
- amelius 9y ago"Promises" are a lot like locks.
- anonacct37 9y agoLookup twisted deferred locks. https://twistedmatrix.com/documents/9.0.0/api/twisted.internet.defer.DeferredLock.html https://twistedmatrix.com/documents/9.0.0/api/twisted.intern...
- nitely 9y agoLooks nice. How is this implemented?
- rdtsc 9y agoNot gp, but I used Twisted Matrix for a few years. It is usually implemented with a boolean flag and a list of client waiting to be called back when the lock is released. So users of the lock would call acquire and get back a deferred (a promise in other language), but say in the most basic way, a callback is passed to it. When lock can be acquired, that callback is called (or deferred is fired). When done with the resource, must call the release. Release then will trigger the lock to inspect the list of available waiting clients, pick one and fire their callback letting them know they can access the resource, and so on. https://github.com/twisted/twisted/blob/trunk/src/twisted/internet/defer.py#L1581 https://github.com/twisted/twisted/blob/trunk/src/twisted/in...
- ChristianBundy 9y agoYep. let lock; const log = (msg) => { lock = new Promise(async (resolve) => { await lock; console.log(msg); setTimeout(resolve, 1000); }); }; 'hello world'.split('').forEach(log);