JavaScript中的互斥体 – 这看起来像是一个正确的实现吗?
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了JavaScript中的互斥体 – 这看起来像是一个正确的实现吗?,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含2639字,纯文字阅读大概需要4分钟。
内容图文
![JavaScript中的互斥体 – 这看起来像是一个正确的实现吗?](/upload/InfoBanner/zyjiaocheng/717/24cec7139e2a46639ad0cc31193dc267.jpg)
这不是一个完全严肃的问题,更多的是淋浴:JavaScript的await关键字应该允许一些非常像平均“并发语言”中的互斥体的东西.
function Mutex() {
var self = this; // still unsure about how "this" is captured
var mtx = new Promise(t => t()); // fulfilled promise ≡ unlocked mutex
this.lock = async function() {
await mtx;
mtx = new Promise(t => {
self.unlock = () => t();
});
}
}
// Lock
await mutex.lock();
// Unlock
mutex.unlock();
这是一个正确的实现(除了正确的错误处理)?而且……我可以拥有C -RAII式锁定装置吗?
解决方法:
您的实施允许尽可能多的消费者获得锁定要求;每次锁定调用都会等待一个承诺:
function Mutex() {
var self = this; // still unsure about how "this" is captured
var mtx = new Promise(t => t()); // fulfilled promise ≡ unlocked mutex
this.lock = async function() {
await mtx;
mtx = new Promise(t => {
self.unlock = () => t();
});
}
}
const mutex = new Mutex();
(async () => {
await Promise.resolve();
await mutex.lock();
console.log("A got the lock");
})();
(async () => {
await Promise.resolve();
await mutex.lock();
console.log("B got the lock");
})();
您需要实现一个promise队列,为每个锁请求创建一个新队列.
附注:
>新的Promise(t => t())可以更简单和惯用地写成Promise.resolve():-)
>如果你使用像这样的箭头功能,就不需要自己;箭头函数关闭它们创建的位置(就像关闭变量一样)
>解锁成为锁定承诺的解析值可能是有意义的,所以只有获得锁定的代码才能释放它
像这样的东西:
function Mutex() {
let current = Promise.resolve();
this.lock = () => {
let _resolve;
const p = new Promise(resolve => {
_resolve = () => resolve();
});
// Caller gets a promise that resolves when the current outstanding
// lock resolves
const rv = current.then(() => _resolve);
// Don't allow the next request until the new promise is done
current = p;
// Return the new promise
return rv;
};
}
实例:
"use strict";
function Mutex() {
let current = Promise.resolve();
this.lock = () => {
let _resolve;
const p = new Promise(resolve => {
_resolve = () => resolve();
});
// Caller gets a promise that resolves when the current outstanding
// lock resolves
const rv = current.then(() => _resolve);
// Don't allow the next request until the new promise is done
current = p;
// Return the new promise
return rv;
};
}
const rand = max => Math.floor(Math.random() * max);
const delay = (ms, value) => new Promise(resolve => setTimeout(resolve, ms, value));
const mutex = new Mutex();
function go(name) {
(async () => {
console.log(name + " random initial delay");
await delay(rand(50));
console.log(name + " requesting lock");
const unlock = await mutex.lock();
console.log(name + " got lock");
await delay(rand(1000));
console.log(name + " releasing lock");
unlock();
})();
}
go("A");
go("B");
go("C");
go("D");
.as-console-wrapper {
max-height: 100% !important;
}
内容总结
以上是互联网集市为您收集整理的JavaScript中的互斥体 – 这看起来像是一个正确的实现吗?全部内容,希望文章能够帮你解决JavaScript中的互斥体 – 这看起来像是一个正确的实现吗?所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。