harmony 鸿蒙SharedArrayBuffer Object
SharedArrayBuffer Object
A SharedArrayBuffer object contains a block of native memory, and its JS object wrapper is allocated in the local heap of the virtual machine. It allows sharing across concurrent instances but requires the use of the Atomics class to manage access and modifications, thereby preventing data races. It is suitable for sharing state or data among multiple concurrent instances. The following figure shows the communication process.
Usage Example
The following is a simple example of using TaskPool to pass an Int32Array object:
import { taskpool } from '@kit.ArkTS';
@Concurrent
function transferAtomics(arg1: Int32Array) {
console.info("wait begin::");
// Use Atomics to perform operations.
let res = Atomics.wait(arg1, 0, 0, 3000);
return res;
}
// Define an object that can be shared.
let sab: SharedArrayBuffer = new SharedArrayBuffer(20);
let int32 = new Int32Array(sab);
let task: taskpool.Task = new taskpool.Task(transferAtomics, int32);
taskpool.execute(task).then((res) => {
console.info("this res is: " + res);
});
setTimeout(() => {
Atomics.notify(int32, 0, 1);
}, 1000);
你可能感兴趣的鸿蒙文章
harmony 鸿蒙Configuring arkOptions in build-profile.json5
harmony 鸿蒙Ark Bytecode File Format
harmony 鸿蒙Naming Conventions for Ark Bytecode Functions
harmony 鸿蒙Ark Bytecode Fundamentals
harmony 鸿蒙Overview of Ark Bytecode
0
赞
- 所属分类: 后端技术
- 本文标签:
热门推荐
-
2、 - 优质文章
-
3、 gate.io
-
8、 golang
-
9、 openharmony
-
10、 Vue中input框自动聚焦