• 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
16// [Start example_pass_obj]
17import { taskpool } from '@kit.ArkTS';
18
19@Concurrent
20function transferAtomics(arg1: Int32Array) {
21  console.info('wait begin::');
22  // 使用Atomics进行操作
23  let res = Atomics.wait(arg1, 0, 0, 3000);
24  return res;
25}
26
27@Entry
28@Component
29struct sharedArrayBuffer {
30  @State message: string = 'Hello World';
31
32  build() {
33    RelativeContainer() {
34      Text(this.message)
35        .id('HelloWorld')
36        .fontSize(50)
37        .fontWeight(FontWeight.Bold)
38        .alignRules({
39          center: { anchor: '__container__', align: VerticalAlign.Center },
40          middle: { anchor: '__container__', align: HorizontalAlign.Center }
41        })
42        .onClick(() => {
43          // 定义可共享对象
44          let sab: SharedArrayBuffer = new SharedArrayBuffer(20);
45          let int32 = new Int32Array(sab);
46          let task: taskpool.Task = new taskpool.Task(transferAtomics, int32);
47          taskpool.execute(task).then((res) => {
48            console.info('this res is: ' + res);
49          });
50          setTimeout(() => {
51            Atomics.notify(int32, 0, 1);
52          }, 1000);
53          this.message = 'success';
54        })
55    }
56    .height('100%')
57    .width('100%')
58  }
59}
60// [End example_pass_obj]
61