目录

Shared Module

A shared module is a module that is loaded only once in a process. The 'use shared' directive is used to mark whether a module is a shared module.

A non-shared module is loaded once in the same thread and multiple times in different threads, creating a new module object in each thread. Currently, only shared modules can be used to implement process-wide singletons.

Constraints

Usage Example

  1. Export a Sendable object from a shared module.

    // Shared module
    import { ArkTSUtils } from '@kit.ArkTS';
    
    // Declare the current module as a shared module, which can only export Sendable data.
    'use shared'
    
    // Shared module, singletonA is globally unique.
    @Sendable
    class SingletonA {
      private count_: number = 0;
      public lock_: ArkTSUtils.locks.AsyncLock = new ArkTSUtils.locks.AsyncLock();
    
      public async getCount(): Promise<number> {
        return this.lock_.lockAsync(() => {
          return this.count_;
        })
      }
    
      public async increaseCount() {
        await this.lock_.lockAsync(() => {
          this.count_++;
        })
      }
    }
    
    export let singletonA = new SingletonA();
  2. Operate an object exported from the shared module across multiple threads.

    import { ArkTSUtils, taskpool } from '@kit.ArkTS';
    import { singletonA } from './sharedModule';
    
    // ...
    
    @Concurrent
    async function increaseCount() {
      await singletonA.increaseCount();
      console.info(`SharedModule: count is: ${await singletonA.getCount()}`);
    }
    
    @Concurrent
    async function printCount() {
      console.info(`SharedModule: count is: ${await singletonA.getCount()}`);
    }
    
    @Entry
    @Component
    struct Index {
      @State message: string = 'Hello World';
      @State mainThreadPrint: string = 'MainThread print count';
      @State taskpoolPrint: string = 'Taskpool print count';
      @State mainThreadIncrease: string = 'MainThread increase count';
      @State taskpoolIncrease: string = 'Taskpool increase count';
    
      build() {
        Row() {
          Column() {
            Button(this.mainThreadPrint)
              .id('MainThread print count')
              .onClick(async () => {
                await printCount();
                this.mainThreadPrint = 'success';
              })
            Button(this.taskpoolPrint)
              .id('Taskpool print count')
              .onClick(async () => {
                await taskpool.execute(printCount);
                this.taskpoolPrint = 'success';
              })
            Button(this.mainThreadIncrease)
              .id('MainThread increase count')
              .onClick(async () => {
                await increaseCount();
                this.mainThreadIncrease = 'success';
              })
            Button(this.taskpoolIncrease)
              .id('Taskpool increase count')
              .onClick(async () => {
                await taskpool.execute(increaseCount);
                this.taskpoolIncrease = 'success';
              })
          }
          .width('100%')
        }
        .height('100%')
      }
    }