aboutsummaryrefslogtreecommitdiff
path: root/files/zh-cn/web/javascript/reference/global_objects/atomics/wait/index.html
blob: 6a14b9260a0c42ec06fec2eca19e3861f4682f07 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
---
title: Atomics.wait()
slug: Web/JavaScript/Reference/Global_Objects/Atomics/wait
tags:
  - Atomics
  - JavaScript
  - Method
  - Shared Memory
translation_of: Web/JavaScript/Reference/Global_Objects/Atomics/wait
---
<div>{{JSRef}}</div>

<p>静态方法 <code><strong>Atomics</strong></code><strong><code>.wait()</code></strong> 确保了一个在 {{jsxref("Int32Array")}} 数组中给定位置的值没有发生变化、仍然是给定的值时进程将会睡眠,直到被唤醒或超时。该方法返回一个字符串,值为<code>"ok"</code>, <code>"not-equal"</code>, 或 <code>"timed-out"</code> 之一。</p>

<div class="note">
<p><strong>注意:</strong> 这项操作仅允许同一个共享内存的 {{jsxref("Int32Array")}} 配合使用并且无法运行在主线程中。</p>
</div>

<h2 id="语法">语法</h2>

<pre class="syntaxbox">Atomics.wait(typedArray, index, value[, timeout])
</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>value</code></dt>
 <dd>给定需要检测的位置索引的预期值。</dd>
 <dt><code>timeout</code> {{optional_inline}}</dt>
 <dd>超时前等待的毫秒数。 {{jsxref("Infinity")}}, 如未提供该参数,将为无穷大。</dd>
</dl>

<h3 id="返回值">返回值</h3>

<p>一个 {{jsxref("String")}} 字符串,值为 "<code>ok</code>", "<code>not-equal</code>", 或 "<code>timed-out</code>" 三种之一。</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">var sab = new SharedArrayBuffer(1024);
var int32 = new Int32Array(sab);
</pre>

<p>检测给定的数组索引0的值,如果它如预期一般的等于我们给定的值0,则这个读取线程将会睡眠等待。一旦当有一个写入线程在这个位置存储了一个新值,它将会收到写入线程的通知并且返回新值 (123) :</p>

<pre class="brush: js">Atomics.wait(int32, 0, 0);
console.log(int32[0]); // 123</pre>

<p>一旦某个写入线程存储了一个新值到<code>int32</code> 的索引0位置,则通知给该等待线程:</p>

<pre class="brush: js">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.wait', 'Atomics.wait')}}</td>
   <td>{{Spec2('ESDraft')}}</td>
   <td>Initial definition in ES2017.</td>
  </tr>
 </tbody>
</table>

<h2 id="浏览器支持">浏览器支持</h2>

<div class="hidden">本页的支持列表生成自结构化的数据。如果你希望为这个数据做出贡献, 请检出 <a href="https://github.com/mdn/browser-compat-data">https://github.com/mdn/browser-compat-data</a> 并发送一个拉取请求给我们。</div>

<p>{{Compat("javascript.builtins.Atomics.wait")}}</p>

<h2 id="相关参阅">相关参阅</h2>

<ul>
 <li>{{jsxref("Atomics")}}</li>
 <li>{{jsxref("Atomics.notify()")}}</li>
</ul>