• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# CommonLibrary Subsystem Changelog
2
3## cl.commonlibrary.1 Behavior for the Task Pool to Transfer Sendable Data Is Changed
4
5**Access Level**
6
7Other
8
9**Change Reason**
10
11The task pool is used to execute concurrent tasks. If Sendable data is directly returned in a call or the **sendData** API is called for data transmission, the default transmission mode of the Sendable data is changed from clone to shared. This change is because the data is seldom used after the task in the task pool is executed.
12
13**Change Impact**
14
15This change is a non-compatible change.
16
17Before the change, when **taskpool.Task.sendData** of API version 11 is used to send or directly return Sendable data, one copy is made by default.
18
19After the change, Sendable data is shared in this scenario. If the data is modified in the host thread, the subthread gets notified. However, the task pool is executed by task. There is a low probability that simultaneous operations are performed on the same object, and the impact of subsequent data changes on the task pool is controllable.
20
21In the following code, the printed value may be 100 or changed to 200 by the subthread:
22
23```
24import { taskpool } from '@kit.ArkTS'
25@Sendable
26class A {
27    num: number = 100
28}
29@Concurrent
30async function foo(a: A) {
31    taskpool.Task.sendData(a)
32    a.num = 200
33}
34let a: A = new A()
35let task = new taskpool.Task(foo, a)
36task.onReceiveData((val: A) => {
37    console.log("num: " + val.num)
38})
39```
40
41**Start API Level**
42
43API version 11
44
45**Change Since**
46
47OpenHarmony SDK 5.0.0.18
48
49**Key API/Component Changes**
50
51N/A
52
53**Adaptation Guide**
54
55If **sendData** is not executed in the task pool or the read and write operations on the same attribute are performed by two independent threads after data is returned, no adaptation is required.
56
57If **sendData** is executed in the task pool or the host thread and its subthread read and write the same attribute after data is returned, a lock must be used (imported from @arkts.utils).
58Current code:
59
60```
61import { taskpool } from '@kit.ArkTS'
62@Sendable
63class A {
64    num: number = 100
65}
66@Concurrent
67async function foo(a: A) {
68    taskpool.Task.sendData(a)
69    a.num = 200
70}
71let a: A = new A()
72let task = new taskpool.Task(foo, a)
73task.onReceiveData((val: A) => {
74    console.log("num: " + val.num)
75})
76```
77To ensure the thread safety of the **num** domain of class A, synchronization protection is required. The recommended code snippet is as follows:
78```
79import { taskpool } from '@kit.ArkTS'
80@Sendable
81class A {
82    num: number = 100
83    async setNum(num: number) {
84        // add lock here
85        this.num = num
86    }
87    async getNum(): Promise<number> {
88        // add lock here
89        return this.num
90    }
91}
92@Concurrent
93async function foo(a: A) {
94    taskpool.Task.sendData(a)
95    a.setNum(200)
96}
97let a: A = new A()
98let task = new taskpool.Task(foo, a)
99task.onReceiveData((val: A) => {
100    console.log("num: " + val.getNum())
101})
102```
103