• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1/*
2 * Copyright (c) 2025 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 *     http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16import { taskpool } from '@kit.ArkTS';
17
18@Concurrent
19function transferAtomics(arg1: Int32Array) {
20  console.info('wait begin::');
21  // 使用Atomics进行操作
22  let res = Atomics.wait(arg1, 0, 0, 3000);
23  return res;
24}
25
26@Entry
27@Component
28struct sharedArrayBuffer {
29  @State message: string = 'Hello World';
30
31  build() {
32    RelativeContainer() {
33      Text(this.message)
34        .id('HelloWorld')
35        .fontSize(50)
36        .fontWeight(FontWeight.Bold)
37        .alignRules({
38          center: { anchor: '__container__', align: VerticalAlign.Center },
39          middle: { anchor: '__container__', align: HorizontalAlign.Center }
40        })
41        .onClick(() => {
42          // 定义可共享对象
43          let sab: SharedArrayBuffer = new SharedArrayBuffer(20);
44          let int32 = new Int32Array(sab);
45          let task: taskpool.Task = new taskpool.Task(transferAtomics, int32);
46          taskpool.execute(task).then((res) => {
47            console.info('this res is: ' + res);
48          });
49          setTimeout(() => {
50            Atomics.notify(int32, 0, 1);
51          }, 1000);
52          this.message = 'success';
53        })
54    }
55    .height('100%')
56    .width('100%')
57  }
58}