diff options
Diffstat (limited to 'files/zh-cn/web/javascript/reference/global_objects/atomics/notify/index.html')
| -rw-r--r-- | files/zh-cn/web/javascript/reference/global_objects/atomics/notify/index.html | 93 |
1 files changed, 93 insertions, 0 deletions
diff --git a/files/zh-cn/web/javascript/reference/global_objects/atomics/notify/index.html b/files/zh-cn/web/javascript/reference/global_objects/atomics/notify/index.html new file mode 100644 index 0000000000..bf2399192c --- /dev/null +++ b/files/zh-cn/web/javascript/reference/global_objects/atomics/notify/index.html @@ -0,0 +1,93 @@ +--- +title: Atomics.notify() +slug: Web/JavaScript/Reference/Global_Objects/Atomics/notify +tags: + - Atomics + - JavaScript + - Method + - Shared Memory +translation_of: Web/JavaScript/Reference/Global_Objects/Atomics/notify +--- +<div>{{JSRef}}</div> + +<p>静态方法 <code><strong>Atomics</strong></code><strong><code>.notify()</code></strong> 提醒一些在等待队列中休眠的代理。</p> + +<div class="note"> +<p><strong>注意:</strong>本操作仅在共享的 {{jsxref("Int32Array")}} 下可用。</p> +</div> + +<h2 id="语法">语法</h2> + +<pre class="syntaxbox notranslate">Atomics.notify(typedArray, index, count) +</pre> + +<h3 id="参数">参数</h3> + +<dl> + <dt><code>typedArray</code></dt> + <dd>一个共享的 {{jsxref("Int32Array")}}。</dd> + <dt><code>index</code></dt> + <dd><code>typedArray</code> 中要唤醒的目标索引。</dd> + <dt><code>count</code></dt> + <dd>要通知的正在休眠的代理的数量。默认是 {{jsxref("Infinity", "+Infinity")}}。</dd> +</dl> + +<h3 id="返回值">返回值</h3> + +<p>被唤醒的代理的数量。</p> + +<h3 id="异常">异常</h3> + +<ul> + <li>若 <code>typedArray</code> 不是共享的 {{jsxref("Int32Array")}},则抛出一个 {{jsxref("TypeError")}} 异常。</li> + <li>若 <code>index</code> 索引超出了 <code>typedArray</code> 的大小,则抛出一个 {{jsxref("RangeError")}} 异常。</li> +</ul> + +<h2 id="示例">示例</h2> + +<p>分配一个共享的 <code>Int32Array</code>:</p> + +<pre class="brush: js notranslate">var sab = new SharedArrayBuffer(1024); +var int32 = new Int32Array(sab); +</pre> + +<p>一个读线程会进入休眠并监视索引0处的值(默认为0)。只要索引0处的值不为0,读进程就会唤醒。但是,一旦写进程存储了一个新的值,写进程就会产生一个提醒并返回写入后的新值(123)。(这里示例有问题或者说对初学者不友好,如果直接在浏览器控制台运行下面代码会报错,因为我们不能尝试睡眠主线程,可以见 <a href="https://github.com/lizhongzhen11/lizz-blog/issues/125#notice">重学js —— 结构化数据之Atomics对象</a>,同时我在 <strong>codepen </strong>写了一个示例:<a href="https://codepen.io/lizhongzhen11/project/editor/AmzyaY#">Atomics.wait使用示例</a>)</p> + +<pre class="brush: js notranslate">Atomics.wait(int32, 0, 0); +console.log(int32[0]); // 123</pre> + +<p>写进程写入一个新值并告知等待进程已经写入成功了:</p> + +<pre class="brush: js notranslate">console.log(int32[0]); // 0; +Atomics.store(int32, 0, 123); +Atomics.notify(int32, 0, 1);</pre> + +<h2 id="规范">规范</h2> + +<table class="standard-table"> + <tbody> + <tr> + <th scope="col">规范</th> + <th scope="col">状态</th> + <th scope="col">注释</th> + </tr> + <tr> + <td>{{SpecName('ESDraft', '#sec-atomics.notify', 'Atomics.notify')}}</td> + <td>{{Spec2('ESDraft')}}</td> + <td>Initial definition in ES2017.</td> + </tr> + </tbody> +</table> + +<h2 id="浏览器兼容性">浏览器兼容性</h2> + + + +<p>{{Compat("javascript.builtins.Atomics.notify")}}</p> + +<h2 id="相关文档">相关文档</h2> + +<ul> + <li>{{jsxref("Atomics")}}</li> + <li>{{jsxref("Atomics.wait()")}}</li> +</ul> |
