harmony 鸿蒙Asynchronous Lock
Asynchronous Lock
To address data race issues in concurrent multithreading tasks, ArkTS introduces asynchronous locks. These locks can be held by class objects. To simplify access across concurrent instances, the AsyncLock object supports pass-by-reference across threads.
Given that ArkTS supports asynchronous operations, blocking locks are prone to deadlocks. Therefore, ArkTS only supports asynchronous locks (non-blocking locks). In addition, asynchronous locks can ensure the temporal consistency of asynchronous tasks within a single thread, preventing synchronization issues caused by uncertain task timing.
For more details about asynchronous lock APIs, see ArkTSUtils.locks.
NOTE
Methods using asynchronous locks must be marked as async, and the caller must use the await keyword to ensure correct timing.
Usage Example
To prevent data races when modifying shared variables across threads with @Sendable objects, asynchronous locks can be used for data protection. The sample code is as follows:
import { ArkTSUtils, taskpool } from '@kit.ArkTS';
@Sendable
export class A {
private count_: number = 0;
lock_: ArkTSUtils.locks.AsyncLock = new ArkTSUtils.locks.AsyncLock();
public async getCount(): Promise<number> {
// Add an asynchronous lock to protect the data.
return this.lock_.lockAsync(() => {
return this.count_;
})
}
public async increaseCount() {
// Add an asynchronous lock to protect the data.
await this.lock_.lockAsync(() => {
this.count_++;
})
}
}
@Concurrent
async function printCount(a: A) {
console.info("InputModule: count is:" + await a.getCount());
}
@Entry
@Component
struct Index {
@State message: string = 'Hello World';
build() {
RelativeContainer() {
Text(this.message)
.id('HelloWorld')
.fontSize(50)
.fontWeight(FontWeight.Bold)
.alignRules({
center: { anchor: '__container__', align: VerticalAlign.Center },
middle: { anchor: '__container__', align: HorizontalAlign.Center }
})
.onClick(async () => {
// Create the Sendable object a.
let a: A = new A();
// Pass object a to a child thread.
await taskpool.execute(printCount, a);
})
}
.height('100%')
.width('100%')
}
}
你可能感兴趣的鸿蒙文章
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
harmony 鸿蒙Asynchronous Waiting
- 所属分类: 后端技术
- 本文标签:
热门推荐
-
2、 - 优质文章
-
3、 gate.io
-
8、 golang
-
9、 openharmony
-
10、 Vue中input框自动聚焦