1e41f4b71Sopenharmony_ci# Passing a Task with the Specified Priority to an ArkTS Thread from an Asynchronous Thread Using Node-API 2e41f4b71Sopenharmony_ciYou can use **napi_call_threadsafe_function_with_priority** to pass a task to an ArkTS thread from an asynchronous thread in a thread-safe manner. Then, the task will be processed based on its priority and enqueuing mode. 3e41f4b71Sopenharmony_ci 4e41f4b71Sopenharmony_ci## Function Description 5e41f4b71Sopenharmony_ci 6e41f4b71Sopenharmony_ci```cpp 7e41f4b71Sopenharmony_cinapi_status napi_call_threadsafe_function_with_priority(napi_threadsafe_function func, 8e41f4b71Sopenharmony_ci void *data, 9e41f4b71Sopenharmony_ci napi_task_priority priority, 10e41f4b71Sopenharmony_ci bool isTail); 11e41f4b71Sopenharmony_ci``` 12e41f4b71Sopenharmony_ci 13e41f4b71Sopenharmony_ci| Name | Description | 14e41f4b71Sopenharmony_ci| :------------- | :----------------------------- | 15e41f4b71Sopenharmony_ci| func | Thread-safe function to call. | 16e41f4b71Sopenharmony_ci| data | Data to be passed from the asynchronous thread to the main thread. | 17e41f4b71Sopenharmony_ci| priority | Priority of the task. For details, see [napi_task_priority](napi-data-types-interfaces.md#thread-safe-task-priority).| 18e41f4b71Sopenharmony_ci| isTail | Whether to add the task to the end (tail) of the task queue. The value **true** means to add the task to the end of the task queue; the value **false** means to add the task to the head of the queue.| 19e41f4b71Sopenharmony_ci 20e41f4b71Sopenharmony_ci## When to Use 21e41f4b71Sopenharmony_ciPass a task to the ArkTS main thread from an asynchronous thread in a thread-safe manner. Then, the task will be processed based on its priority and enqueuing mode. 22e41f4b71Sopenharmony_ci 23e41f4b71Sopenharmony_ci## Calling an ArkTS API Asynchronously 24e41f4b71Sopenharmony_ci 25e41f4b71Sopenharmony_ci### Example 26e41f4b71Sopenharmony_ci 27e41f4b71Sopenharmony_ci- Register the module. 28e41f4b71Sopenharmony_ci 29e41f4b71Sopenharmony_ci ```c++ 30e41f4b71Sopenharmony_ci // hello.cpp 31e41f4b71Sopenharmony_ci #include "napi/native_api.h" 32e41f4b71Sopenharmony_ci #include <string.h> 33e41f4b71Sopenharmony_ci #include <stdlib.h> 34e41f4b71Sopenharmony_ci 35e41f4b71Sopenharmony_ci struct CallbackData { 36e41f4b71Sopenharmony_ci napi_threadsafe_function tsfn; 37e41f4b71Sopenharmony_ci napi_async_work work; 38e41f4b71Sopenharmony_ci }; 39e41f4b71Sopenharmony_ci // Callback implementation in the ArkTS thread. 40e41f4b71Sopenharmony_ci static void CallJs(napi_env env, napi_value jsCb, void *context, void *data) { 41e41f4b71Sopenharmony_ci if (env == nullptr) { 42e41f4b71Sopenharmony_ci return; 43e41f4b71Sopenharmony_ci } 44e41f4b71Sopenharmony_ci napi_value resultNumber = nullptr; 45e41f4b71Sopenharmony_ci napi_value undefined = nullptr; 46e41f4b71Sopenharmony_ci napi_get_undefined(env, &undefined); 47e41f4b71Sopenharmony_ci napi_value number1 = nullptr; 48e41f4b71Sopenharmony_ci napi_create_int32(env, 12, &number1); 49e41f4b71Sopenharmony_ci napi_value number2 = nullptr; 50e41f4b71Sopenharmony_ci napi_create_int32(env, 15, &number2); 51e41f4b71Sopenharmony_ci napi_value argv[2] = {number1, number2}; 52e41f4b71Sopenharmony_ci napi_call_function(env, undefined, jsCb, 2, argv, &resultNumber); 53e41f4b71Sopenharmony_ci int32_t res = 0; 54e41f4b71Sopenharmony_ci napi_get_value_int32(env, resultNumber, &res); 55e41f4b71Sopenharmony_ci } 56e41f4b71Sopenharmony_ci 57e41f4b71Sopenharmony_ci // Call this API in an asynchronous thread to pass a task with the specified priority and enqueuing mode to an ArkTS thread. 58e41f4b71Sopenharmony_ci static void ExecuteWork(napi_env env, void *data) { 59e41f4b71Sopenharmony_ci CallbackData *callbackData = reinterpret_cast<CallbackData *>(data); 60e41f4b71Sopenharmony_ci // The task priority is napi_priority_idle, and the task is added to the end of the task queue. 61e41f4b71Sopenharmony_ci napi_call_threadsafe_function_with_priority(callbackData->tsfn, nullptr, napi_priority_idle, true); 62e41f4b71Sopenharmony_ci napi_call_threadsafe_function_with_priority(callbackData->tsfn, nullptr, napi_priority_low, true); 63e41f4b71Sopenharmony_ci napi_call_threadsafe_function_with_priority(callbackData->tsfn, nullptr, napi_priority_high, true); 64e41f4b71Sopenharmony_ci napi_call_threadsafe_function_with_priority(callbackData->tsfn, nullptr, napi_priority_immediate, true); 65e41f4b71Sopenharmony_ci // The task priority is napi_priority_high, and the task is added to the head of the task queue. 66e41f4b71Sopenharmony_ci napi_call_threadsafe_function_with_priority(callbackData->tsfn, nullptr, napi_priority_high, false); 67e41f4b71Sopenharmony_ci } 68e41f4b71Sopenharmony_ci 69e41f4b71Sopenharmony_ci static void WorkComplete(napi_env env, napi_status status, void *data) { 70e41f4b71Sopenharmony_ci CallbackData *callbackData = reinterpret_cast<CallbackData *>(data); 71e41f4b71Sopenharmony_ci napi_release_threadsafe_function(callbackData->tsfn, napi_tsfn_release); 72e41f4b71Sopenharmony_ci napi_delete_async_work(env, callbackData->work); 73e41f4b71Sopenharmony_ci callbackData->work = nullptr; 74e41f4b71Sopenharmony_ci callbackData->tsfn = nullptr; 75e41f4b71Sopenharmony_ci } 76e41f4b71Sopenharmony_ci 77e41f4b71Sopenharmony_ci static napi_value CallThreadSafeWithPriority(napi_env env, napi_callback_info info) { 78e41f4b71Sopenharmony_ci size_t argc = 1; 79e41f4b71Sopenharmony_ci napi_value jsCb = nullptr; 80e41f4b71Sopenharmony_ci CallbackData *callbackData = nullptr; 81e41f4b71Sopenharmony_ci napi_get_cb_info(env, info, &argc, &jsCb, nullptr, reinterpret_cast<void **>(&callbackData)); 82e41f4b71Sopenharmony_ci napi_value resourceName = nullptr; 83e41f4b71Sopenharmony_ci napi_create_string_utf8(env, "Thread-safe Function Demo", NAPI_AUTO_LENGTH, &resourceName); 84e41f4b71Sopenharmony_ci napi_create_threadsafe_function(env, jsCb, nullptr, resourceName, 0, 1, callbackData, nullptr, callbackData, CallJs, 85e41f4b71Sopenharmony_ci &callbackData->tsfn); 86e41f4b71Sopenharmony_ci napi_create_async_work(env, nullptr, resourceName, ExecuteWork, WorkComplete, callbackData, &callbackData->work); 87e41f4b71Sopenharmony_ci napi_queue_async_work(env, callbackData->work); 88e41f4b71Sopenharmony_ci return nullptr; 89e41f4b71Sopenharmony_ci } 90e41f4b71Sopenharmony_ci 91e41f4b71Sopenharmony_ci // Register the module. 92e41f4b71Sopenharmony_ci EXTERN_C_START 93e41f4b71Sopenharmony_ci static napi_value Init(napi_env env, napi_value exports) 94e41f4b71Sopenharmony_ci { 95e41f4b71Sopenharmony_ci CallbackData *callbackData = new CallbackData(); 96e41f4b71Sopenharmony_ci napi_property_descriptor desc[] = { 97e41f4b71Sopenharmony_ci { "callThreadSafeWithPriority", nullptr, CallThreadSafeWithPriority, nullptr, nullptr, nullptr, napi_default, callbackData } 98e41f4b71Sopenharmony_ci }; 99e41f4b71Sopenharmony_ci napi_define_properties(env, exports, sizeof(desc) / sizeof(desc[0]), desc); 100e41f4b71Sopenharmony_ci return exports; 101e41f4b71Sopenharmony_ci } 102e41f4b71Sopenharmony_ci EXTERN_C_END 103e41f4b71Sopenharmony_ci 104e41f4b71Sopenharmony_ci static napi_module nativeModule = { 105e41f4b71Sopenharmony_ci .nm_version = 1, 106e41f4b71Sopenharmony_ci .nm_flags = 0, 107e41f4b71Sopenharmony_ci .nm_filename = nullptr, 108e41f4b71Sopenharmony_ci .nm_register_func = Init, 109e41f4b71Sopenharmony_ci .nm_modname = "entry", 110e41f4b71Sopenharmony_ci .nm_priv = nullptr, 111e41f4b71Sopenharmony_ci .reserved = { 0 }, 112e41f4b71Sopenharmony_ci }; 113e41f4b71Sopenharmony_ci 114e41f4b71Sopenharmony_ci extern "C" __attribute__((constructor)) void RegisterEntryModule() 115e41f4b71Sopenharmony_ci { 116e41f4b71Sopenharmony_ci napi_module_register(&nativeModule); 117e41f4b71Sopenharmony_ci } 118e41f4b71Sopenharmony_ci ``` 119e41f4b71Sopenharmony_ci 120e41f4b71Sopenharmony_ci- Declare the API. 121e41f4b71Sopenharmony_ci 122e41f4b71Sopenharmony_ci ```ts 123e41f4b71Sopenharmony_ci // index.d.ts 124e41f4b71Sopenharmony_ci export const callThreadSafeWithPriority: (cb: (a: number, b: number) => number) => void; 125e41f4b71Sopenharmony_ci ``` 126e41f4b71Sopenharmony_ci 127e41f4b71Sopenharmony_ci- Configure compile settings. 128e41f4b71Sopenharmony_ci 129e41f4b71Sopenharmony_ci Configure the **CMakeLists.txt** file as follows: 130e41f4b71Sopenharmony_ci 131e41f4b71Sopenharmony_ci ``` 132e41f4b71Sopenharmony_ci // CMakeLists.txt 133e41f4b71Sopenharmony_ci # the minimum version of CMake. 134e41f4b71Sopenharmony_ci cmake_minimum_required(VERSION 3.4.1) 135e41f4b71Sopenharmony_ci project(myapplication) 136e41f4b71Sopenharmony_ci 137e41f4b71Sopenharmony_ci set(NATIVERENDER_ROOT_PATH ${CMAKE_CURRENT_SOURCE_DIR}) 138e41f4b71Sopenharmony_ci 139e41f4b71Sopenharmony_ci if(DEFINED PACKAGE_FIND_FILE) 140e41f4b71Sopenharmony_ci include(${PACKAGE_FIND_FILE}) 141e41f4b71Sopenharmony_ci endif() 142e41f4b71Sopenharmony_ci 143e41f4b71Sopenharmony_ci include_directories(${NATIVERENDER_ROOT_PATH} 144e41f4b71Sopenharmony_ci ${NATIVERENDER_ROOT_PATH}/include) 145e41f4b71Sopenharmony_ci add_library(entry SHARED hello.cpp) 146e41f4b71Sopenharmony_ci target_link_libraries(entry PUBLIC libace_napi.z.so) 147e41f4b71Sopenharmony_ci ``` 148e41f4b71Sopenharmony_ci 149e41f4b71Sopenharmony_ci- ArkTS sample code 150e41f4b71Sopenharmony_ci 151e41f4b71Sopenharmony_ci ```ts 152e41f4b71Sopenharmony_ci // index.ets 153e41f4b71Sopenharmony_ci import testNapi from 'libentry.so' 154e41f4b71Sopenharmony_ci 155e41f4b71Sopenharmony_ci let callback = (a: number, b: number) : number => { 156e41f4b71Sopenharmony_ci console.info('result is ' + (a + b)) 157e41f4b71Sopenharmony_ci return a + b; 158e41f4b71Sopenharmony_ci } 159e41f4b71Sopenharmony_ci testNapi.callThreadSafeWithPriority(callback); 160e41f4b71Sopenharmony_ci ``` 161