最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

HarmonyOS中使用Node-API開(kāi)發(fā)的典型場(chǎng)景示例

 更新時(shí)間:2025年09月13日 10:09:09   作者:ChinaDragon10  
在Native側(cè)C/C++開(kāi)發(fā),計(jì)算密集型場(chǎng)景,對(duì)需要執(zhí)行耗時(shí)操作的邏輯,為避免阻塞應(yīng)用側(cè)主線程,確保應(yīng)用程序的性能和響應(yīng)效率,開(kāi)發(fā)者需要將該部分業(yè)務(wù)邏輯設(shè)計(jì)在Native側(cè)進(jìn)行異步執(zhí)行,本文基于ArkTS多線程場(chǎng)景,講解同步、callback、promise異步模型及線程安全開(kāi)發(fā)的機(jī)制與流程

一、引言

在Native側(cè)C/C++開(kāi)發(fā)場(chǎng)景中,對(duì)于計(jì)算簡(jiǎn)單、應(yīng)用側(cè)主線程需要實(shí)時(shí)等待結(jié)果的情況下,開(kāi)發(fā)者往往會(huì)采用常見(jiàn)的同步方式開(kāi)發(fā)業(yè)務(wù)邏輯。然而,在計(jì)算密集型場(chǎng)景中,對(duì)于需要執(zhí)行耗時(shí)操作的邏輯,為了避免阻塞應(yīng)用側(cè)主線程,確保應(yīng)用程序的性能和響應(yīng)效率,開(kāi)發(fā)者需要將該部分業(yè)務(wù)邏輯設(shè)計(jì)在Native側(cè)進(jìn)行異步執(zhí)行。在異步開(kāi)發(fā)中,開(kāi)發(fā)者需要將C/C++子線程異步處理的結(jié)果反饋到ArkTS主線程,用以應(yīng)用側(cè)UI界面刷新。以下是基于ArkTS的多線程場(chǎng)景開(kāi)發(fā)案例,詳細(xì)講解同步開(kāi)發(fā)、callback異步模型開(kāi)發(fā)、promise異步模型開(kāi)發(fā)以及線程安全開(kāi)發(fā)等典型場(chǎng)景的機(jī)制原理和開(kāi)發(fā)流程。

二、典型開(kāi)發(fā)場(chǎng)景概述

2.1 使用Node-API進(jìn)行同步任務(wù)開(kāi)發(fā)

在同步任務(wù)開(kāi)發(fā)中,ArkTS應(yīng)用側(cè)主線程將阻塞等待Native側(cè)計(jì)算結(jié)果,Native側(cè)計(jì)算結(jié)果通過(guò)方舟引擎反饋給ArkTS應(yīng)用側(cè)。此過(guò)程中,Native接口代碼與ArkTS應(yīng)用側(cè)均運(yùn)行在ArkTS主線程上。例如,應(yīng)用側(cè)需要讀取文件,阻塞等待Native側(cè)文件讀取完成,然后再繼續(xù)運(yùn)行。類(lèi)似地,異步模式下,應(yīng)用側(cè)將收到臨時(shí)結(jié)果,并繼續(xù)執(zhí)行UI操作。

從上圖可以看出,當(dāng)Native同步任務(wù)接口被調(diào)用時(shí),該接口會(huì)完成參數(shù)解析、數(shù)據(jù)類(lèi)型轉(zhuǎn)換、創(chuàng)建生產(chǎn)者和消費(fèi)者線程等。在等待生產(chǎn)者和消費(fèi)者線程執(zhí)行結(jié)束后,將圖片路徑結(jié)果轉(zhuǎn)換為napi_value,由方舟引擎直接反饋到ArkTS應(yīng)用側(cè)。

2.2 使用Node-API進(jìn)行異步任務(wù)開(kāi)發(fā)

在異步任務(wù)開(kāi)發(fā)中,應(yīng)用側(cè)在調(diào)用Native接口后,將會(huì)收到臨時(shí)結(jié)果,并繼續(xù)執(zhí)行UI操作。Native側(cè)將異步執(zhí)行業(yè)務(wù)邏輯,不阻塞應(yīng)用側(cè)。例如,應(yīng)用側(cè)需要讀取文件,異步模式下,應(yīng)用側(cè)將不會(huì)等待Native側(cè)文件讀取完成,并繼續(xù)運(yùn)行。

從上圖可以看出,當(dāng)Native異步任務(wù)接口被調(diào)用時(shí),該接口會(huì)完成參數(shù)解析、數(shù)據(jù)類(lèi)型轉(zhuǎn)換、異步工作項(xiàng)創(chuàng)建、異步任務(wù)壓入調(diào)度隊(duì)列以及返回空值(Callback方式)或者Promise對(duì)象(Promise方式)等。異步任務(wù)的調(diào)度、執(zhí)行以及反饋計(jì)算結(jié)果,均由libuv線程池和EventLoop機(jī)制進(jìn)行系統(tǒng)調(diào)度完成。

2.3 使用Node-API進(jìn)行線程安全開(kāi)發(fā)

在異步多線程場(chǎng)景中,如果需要進(jìn)行耗時(shí)的計(jì)算或IO操作,或者需要在多個(gè)線程之間共享數(shù)據(jù),可以創(chuàng)建一個(gè)線程安全函數(shù),確保任務(wù)執(zhí)行過(guò)程中的線程安全。例如,異步計(jì)算:如果需要進(jìn)行耗時(shí)的計(jì)算或IO操作,可以創(chuàng)建一個(gè)線程安全函數(shù),將計(jì)算或IO操作放在另一個(gè)線程中執(zhí)行,避免阻塞主線程,提高程序的響應(yīng)速度。數(shù)據(jù)共享:如果多個(gè)線程需要訪問(wèn)同一份數(shù)據(jù),可以創(chuàng)建一個(gè)線程安全函數(shù),確保數(shù)據(jù)的讀寫(xiě)操作不會(huì)發(fā)生競(jìng)爭(zhēng)條件或死鎖等問(wèn)題。

三、開(kāi)發(fā)案例概述

本文將以一個(gè)圖片加載的案例為背景來(lái)詳細(xì)講解上述幾種典型場(chǎng)景的開(kāi)發(fā)步驟和關(guān)鍵API使用方法。在此案例中,用戶(hù)將通過(guò)UI界面上呈現(xiàn)的按鈕分別選擇對(duì)應(yīng)的典型場(chǎng)景接口進(jìn)行圖片加載。

3.1 案例設(shè)計(jì)思路

本案例的實(shí)現(xiàn)分為ArkTS應(yīng)用側(cè)和Native側(cè)兩個(gè)部分,如下圖所示:

ArkTS應(yīng)用側(cè)

提供多種典型場(chǎng)景接口按鈕,以供用戶(hù)進(jìn)行場(chǎng)景選擇。分別為同步調(diào)用、callback異步調(diào)用、promise異步調(diào)用、tsf(線程安全函數(shù))異步調(diào)用以及錯(cuò)誤圖片。不同的按鈕觸發(fā)后,將會(huì)調(diào)用相應(yīng)的Native接口進(jìn)行同步或者異步處理,獲取圖片路徑信息,從而刷新UI界面。其中,錯(cuò)誤圖片按鈕反饋的是一個(gè)錯(cuò)誤圖片路徑,觸發(fā)后,界面上將會(huì)彈出一個(gè)錯(cuò)誤提示彈窗。

Native側(cè)

根據(jù)應(yīng)用側(cè)觸發(fā)的不同場(chǎng)景,將會(huì)執(zhí)行不同的接口實(shí)現(xiàn)。整個(gè)業(yè)務(wù)邏輯分為以下兩個(gè)部分:

  • Native接口,該部分主要實(shí)現(xiàn)Native接口的同步處理或者異步處理邏輯。
  • 同步處理,Native接口將直接調(diào)用圖片路徑搜索功能,將應(yīng)用側(cè)傳入的圖片名稱(chēng)輸入到生產(chǎn)者-消費(fèi)者模型中,進(jìn)行相應(yīng)圖片路徑獲取,最后反饋到ArkTS應(yīng)用側(cè)。
  • 異步處理,Native接口將通過(guò)異步工作項(xiàng)或者線程安全函數(shù)進(jìn)行異步任務(wù)創(chuàng)建,將應(yīng)用側(cè)傳入的圖片名稱(chēng)輸入到上下文數(shù)據(jù)對(duì)象中,待后續(xù)異步任務(wù)調(diào)度處理。在異步任務(wù)處理中,同樣調(diào)度生產(chǎn)者-消費(fèi)者模型進(jìn)行圖片路徑獲取,最后通過(guò)線程通信將結(jié)果反饋到ArkTS應(yīng)用側(cè)。
  • 生產(chǎn)者-消費(fèi)者模型,該部分邏輯主要通過(guò)C++子線程、信號(hào)量以及條件變量等關(guān)鍵特性來(lái)實(shí)現(xiàn)。
  • 生產(chǎn)者線程負(fù)責(zé)根據(jù)圖片名稱(chēng)搜索目標(biāo)圖片路徑。并且將搜索結(jié)果放入緩沖隊(duì)列中。
  • 消費(fèi)者線程負(fù)責(zé)從緩沖隊(duì)列中取出目標(biāo)圖片路徑,并通過(guò)線程通信的方式將結(jié)果反饋到ArkTS應(yīng)用側(cè)。

案例流程圖

案例效果圖

3.2 生產(chǎn)者-消費(fèi)者模型實(shí)現(xiàn)

1. 模型緩沖隊(duì)列ProducerConsumer.h 頭文件

#ifndef MultiThreads_ProducerConsumer_H
#define MultiThreads_ProducerConsumer_H


#include <string>
#include <queue>
#include <mutex>
#include <condition_variable>


using namespace std;
// Principles of the producer-consumer model: Use buffer zones to balance the rate between production and consumption.
// Synchronization relationship 1: When the buffer is full, the producer needs to be blocked and wait. When a product
// pops up the buffer, the producer needs to be woken up for consumption. Synchronization relationship 2: When the
// buffer is empty, the consumer needs to be blocked and waited. When a product enters the buffer, the consumer needs to
// be woken up for consumption.
class ProducerConsumerQueue {
public:
    // constructor
    ProducerConsumerQueue() {}
    ProducerConsumerQueue(int queueSize) : m_maxSize(queueSize) {}
    // producer enqueue operation
    void PutElement(string element);
    // consumer dequeue operation
    string TakeElement();
private:
    // check whether the buffer queue is full
    bool isFull() { return (m_queue.size() == m_maxSize); }
    // check whether the buffer queue is empty
    bool isEmpty() { return m_queue.empty(); }
private:
    queue<string> m_queue{};         // buffer queue
    int m_maxSize{};                 // buffer queue capacity
    mutex m_mutex{};                 // the mutex is used to protect data consistency
    condition_variable m_notEmpty{}; // condition variable, which is used to indicate whether the buffer queue is empty
    condition_variable m_notFull{};  // condition variable, which is used to indicate whether the buffer queue is full
};
#endif // MultiThreads_ProducerConsumer_H

ProducerConsumer.cpp 源文件

#include "ProducerConsumer.h"


void ProducerConsumerQueue::PutElement(string element) {
    unique_lock<mutex> lock(m_mutex); // add mutex


    while (isFull()) {
        // when the data buffer queue is full, the production is blocked and wakes up after consumer consumption
        // m_mutex is automatically released at the same time
        m_notFull.wait(lock);
    }
    // reacquire the lock
    // if the queue is not full
    // the product is added to the queue and the consumer is notified that the product can be consumed
    m_queue.push(element);
    m_notEmpty.notify_one();
}
string ProducerConsumerQueue::TakeElement() {
    unique_lock<mutex> lock(m_mutex); // add mutex


    while (isEmpty()) {
        // when the data buffer queue is empty, the consumption is blocked and the producer is woken up after production
        // m_mutex is automatically released at the same time
        m_notEmpty.wait(lock);
    }
    // reacquire the lock
    // if the queue is not empty, the product is ejected and the producer is notified that it is ready for production
    string element = m_queue.front();
    m_queue.pop();
    m_notFull.notify_one();
    return element;
}

2. 全局變量及搜索接口定義

定義一個(gè)靜態(tài)全局的模型緩沖隊(duì)列。由于,每次只處理一張圖片,所以將容量設(shè)定為1。
定義一個(gè)靜態(tài)全局的圖片路徑集合,用于后文搜索。

MultiThreads.cpp文件

/*
 * Copyright (c) 2024 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include "napi/native_api.h"
#include "ProducerConsumer.h"
#include <vector>
#include <thread>

using namespace std;

// define global thread safe function
static napi_threadsafe_function tsFun = nullptr;

static constexpr int MAX_MSG_QUEUE_SIZE = 0; // indicates that the queue length is not limited
static constexpr int INITIAL_THREAD_COUNT = 1;

// context data provided by users
// data is transferred between the native method (initialization data), ExecuteFunc, and CompleteFunc
struct ContextData {
    napi_async_work asyncWork = nullptr; // async work object
    napi_deferred deferred = nullptr;    // associated object of the delay object promise
    napi_ref callbackRef = nullptr;      // reference of callback
    string args = "";                    // parameters from ArkTS --- imageName
    string result = "";                  // C++ sub-thread calculation result --- imagePath
};

// define the buffer queue and set the capacity to 1
static ProducerConsumerQueue buffQueue(1);

// defines the image path set, which is used for later search
static vector<string> imagePathVec{"sync.png", "callback.png", "promise.png", "tsf.png"};

// check the image paths based on the image name
static bool CheckImagePath(const string &imageName, const string &imagePath) {
    // separate character strings by suffix
    size_t pos = imagePath.find_first_of('.');
    if (pos == string::npos) {
        return false;
    }

    string nameTemp = imagePath.substr(0, pos);
    if (nameTemp.empty()) {
        return false;
    }

    // determine whether the image path is the target path based on whether the image names are the same
    return (imageName == nameTemp);
}

// search for image paths by image name
static string SearchImagePath(const string &imageName) {
    for (const string &imagePath : imagePathVec) {
        if (CheckImagePath(imageName, imagePath)) {
            return imagePath;
        }
    }

    return string("");
}

static void ProductElement(void *data) {
    buffQueue.PutElement(SearchImagePath(static_cast<ContextData *>(data)->args));
}

static void ConsumeElement(void *data) { static_cast<ContextData *>(data)->result = buffQueue.TakeElement(); }

static void ConsumeElementTSF(void *data) {
    static_cast<ContextData *>(data)->result = buffQueue.TakeElement();
    // bind consumer thread to the thread safe function
    (void)napi_acquire_threadsafe_function(tsFun);
    // send async task to the JS main thread EventLoop
    (void)napi_call_threadsafe_function(tsFun, data, napi_tsfn_blocking);
    // release thread reference
    (void)napi_release_threadsafe_function(tsFun, napi_tsfn_release);
}

static void DeleteContext(napi_env env, ContextData *contextData) {
    // delete callback reference
    if (contextData->callbackRef != nullptr) {
        (void)napi_delete_reference(env, contextData->callbackRef);
    }

    // delete async work
    if (contextData->asyncWork != nullptr) {
        (void)napi_delete_async_work(env, contextData->asyncWork);
    }

    // release context data
    delete contextData;
}

static void ExecuteFunc([[maybe_unused]] napi_env env, void *data) {
    // create producer thread
    thread producer(ProductElement, data);
    // the producer and consumer threads must be synchronized
    // otherwise, the complete operation is triggered to communicate with the ArkTS after the executeFunc is complete
    // the result is unpredictable
    producer.join();

    // create consumer thread
    thread consumer(ConsumeElement, data);
    consumer.join();
}

static void CompleteFuncCallBack(napi_env env, [[maybe_unused]] napi_status status, void *data) {
    // parse context data
    ContextData *contextData = static_cast<ContextData *>(data);

    napi_value callBack = nullptr;
    napi_status operStatus = napi_get_reference_value(env, contextData->callbackRef, &callBack);
    if (operStatus != napi_ok) {
        DeleteContext(env, contextData);
        return;
    }

    // define the undefined variable, which is used in napi_call_function
    // because no other data is transferred, the variable is defined as undefined
    napi_value undefined = nullptr;
    operStatus = napi_get_undefined(env, &undefined);
    if (operStatus != napi_ok) {
        DeleteContext(env, contextData);
        return;
    }

    // convert the calculation result of C++ sub-thread to the napi_value type
    napi_value callBackArgs = nullptr;
    operStatus = napi_create_string_utf8(env, contextData->result.c_str(), contextData->result.length(), &callBackArgs);
    if (operStatus != napi_ok) {
        DeleteContext(env, contextData);
        return;
    }

    // call the JS callback and send the async calculation result on the Native to ArkTS application
    napi_value callBackResult = nullptr;
    (void)napi_call_function(env, undefined, callBack, 1, &callBackArgs, &callBackResult);

    // destroy data and release memory
    DeleteContext(env, contextData);
}

static void CompleteFuncPromise(napi_env env, [[maybe_unused]] napi_status status, void *data) {
    // parse context data
    ContextData *contextData = static_cast<ContextData *>(data);

    // convert the calculation result of C++ sub-thread to the napi_value type
    napi_value promiseArgs = nullptr;
    napi_status operStatus =
        napi_create_string_utf8(env, contextData->result.c_str(), contextData->result.length(), &promiseArgs);
    if (operStatus != napi_ok) {
        DeleteContext(env, contextData);
        return;
    }

    // the deferred and promise object are associated. the result is sent to ArkTS application through this interface
    operStatus = napi_resolve_deferred(env, contextData->deferred, promiseArgs);
    if (operStatus != napi_ok) {
        DeleteContext(env, contextData);
        return;
    }

    // destroy data and release memory
    DeleteContext(env, contextData);
}

static void CallJsFunction(napi_env env, napi_value callBack, [[maybe_unused]] void *context, void *data) {
    // parse context data
    ContextData *contextData = static_cast<ContextData *>(data);

    // define the undefined variable, which is used in napi_call_function
    // because no other data is transferred, the variable is defined as undefined
    napi_value undefined = nullptr;
    napi_status operStatus = napi_get_undefined(env, &undefined);
    if (operStatus != napi_ok) {
        DeleteContext(env, contextData);
        return;
    }

    // convert the calculation result of C++ sub-thread to the napi_value type
    napi_value callBackArgs = nullptr;
    operStatus = napi_create_string_utf8(env, contextData->result.c_str(), contextData->result.length(), &callBackArgs);
    if (operStatus != napi_ok) {
        DeleteContext(env, contextData);
        return;
    }

    // call the JS callback and send the async calculation result on the Native to ArkTS application
    napi_value callBackResult = nullptr;
    (void)napi_call_function(env, undefined, callBack, 1, &callBackArgs, &callBackResult);

    // destroy data and release memory
    DeleteContext(env, contextData);
}

// sync interface
static napi_value GetImagePathSync(napi_env env, napi_callback_info info) {
    size_t paraNum = 1;
    napi_value paraArray[1] = {nullptr};

    // parse parameters
    napi_status operStatus = napi_get_cb_info(env, info, ?Num, paraArray, nullptr, nullptr);
    if (operStatus != napi_ok) {
        return nullptr;
    }

    napi_valuetype paraDataType = napi_undefined;
    operStatus = napi_typeof(env, paraArray[0], ?DataType);
    if ((operStatus != napi_ok) || (paraDataType != napi_string)) {
        return nullptr;
    }

    // convert napi_value to char *
    constexpr size_t buffSize = 100;
    char strBuff[buffSize]{}; // char buffer for imageName string
    size_t strLength = 0;
    operStatus = napi_get_value_string_utf8(env, paraArray[0], strBuff, buffSize, &strLength);
    if ((operStatus != napi_ok) || (strLength == 0)) {
        return nullptr;
    }

    // defines context data. the memory will be released in CompleteFunc
    auto contextData = new ContextData;
    contextData->args = strBuff;

    // create producer thread
    thread producer(ProductElement, static_cast<void *>(contextData));
    producer.join();

    // create consumer thread
    thread consumer(ConsumeElement, static_cast<void *>(contextData));
    consumer.join();

    // convert the result to napi_value and send it to ArkTs application
    napi_value result = nullptr;
    (void)napi_create_string_utf8(env, contextData->result.c_str(), contextData->result.length(), &result);

    // delete context data
    DeleteContext(env, contextData);
    return result;
}

// callback async interface
static napi_value GetImagePathAsyncCallBack(napi_env env, napi_callback_info info) {
    size_t paraNum = 2;
    napi_value paraArray[2] = {nullptr};

    // parse parameters
    napi_status operStatus = napi_get_cb_info(env, info, ?Num, paraArray, nullptr, nullptr);
    if (operStatus != napi_ok) {
        return nullptr;
    }

    napi_valuetype paraDataType = napi_undefined;
    operStatus = napi_typeof(env, paraArray[0], ?DataType);
    if ((operStatus != napi_ok) || (paraDataType != napi_string)) {
        return nullptr;
    }

    operStatus = napi_typeof(env, paraArray[1], ?DataType);
    if ((operStatus != napi_ok) || (paraDataType != napi_function)) {
        return nullptr;
    }

    // napi_value convert to char *
    constexpr size_t buffSize = 100;
    char strBuff[buffSize]{}; // char buffer for imageName string
    size_t strLength = 0;
    operStatus = napi_get_value_string_utf8(env, paraArray[0], strBuff, buffSize, &strLength);
    if ((operStatus != napi_ok) || (strLength == 0)) {
        return nullptr;
    }

    // defines context data. the memory will be released in CompleteFunc
    auto contextData = new ContextData;
    contextData->args = strBuff;
    operStatus = napi_create_reference(env, paraArray[1], 1, &contextData->callbackRef);
    if (operStatus != napi_ok) {
        DeleteContext(env, contextData);
        return nullptr;
    }

    // async resource
    napi_value asyncName = nullptr;
    string asyncStr = "async callback";
    operStatus = napi_create_string_utf8(env, asyncStr.c_str(), asyncStr.length(), &asyncName);
    if (operStatus != napi_ok) {
        DeleteContext(env, contextData);
        return nullptr;
    }

    // create async work
    operStatus = napi_create_async_work(env, nullptr, asyncName, ExecuteFunc, CompleteFuncCallBack,
                                        static_cast<void *>(contextData), &contextData->asyncWork);
    if (operStatus != napi_ok) {
        DeleteContext(env, contextData);
        return nullptr;
    }

    // add the async work to the queue and wait for scheduling
    operStatus = napi_queue_async_work(env, contextData->asyncWork);
    if (operStatus != napi_ok) {
        DeleteContext(env, contextData);
    }

    return nullptr;
}

// promise async interface
static napi_value GetImagePathAsyncPromise(napi_env env, napi_callback_info info) {
    size_t paraNum = 1;
    napi_value paraArray[1] = {nullptr};

    // parse parameters
    napi_status operStatus = napi_get_cb_info(env, info, ?Num, paraArray, nullptr, nullptr);
    if (operStatus != napi_ok) {
        return nullptr;
    }

    napi_valuetype paraDataType = napi_undefined;
    operStatus = napi_typeof(env, paraArray[0], ?DataType);
    if ((operStatus != napi_ok) || (paraDataType != napi_string)) {
        return nullptr;
    }

    // napi_value convert to char *
    constexpr size_t buffSize = 100;
    char strBuff[buffSize]{}; // char buffer for imageName string
    size_t strLength = 0;
    operStatus = napi_get_value_string_utf8(env, paraArray[0], strBuff, buffSize, &strLength);
    if ((operStatus != napi_ok) || (strLength == 0)) {
        return nullptr;
    }

    // defines context data. the memory will be released in CompleteFunc
    auto contextData = new ContextData;
    contextData->args = strBuff;

    // async resource
    napi_value asyncName = nullptr;
    string asyncStr = "async promise";
    operStatus = napi_create_string_utf8(env, asyncStr.c_str(), asyncStr.length(), &asyncName);
    if (operStatus != napi_ok) {
        DeleteContext(env, contextData);
        return nullptr;
    }

    // create async work
    operStatus = napi_create_async_work(env, nullptr, asyncName, ExecuteFunc, CompleteFuncPromise,
                                        static_cast<void *>(contextData), &contextData->asyncWork);
    if (operStatus != napi_ok) {
        DeleteContext(env, contextData);
        return nullptr;
    }

    // add the async work to the queue and wait for scheduling
    operStatus = napi_queue_async_work(env, contextData->asyncWork);
    if (operStatus != napi_ok) {
        DeleteContext(env, contextData);
        return nullptr;
    }

    // create promise object
    napi_value promiseObj = nullptr;
    operStatus = napi_create_promise(env, &contextData->deferred, &promiseObj);
    if (operStatus != napi_ok) {
        DeleteContext(env, contextData);
        return nullptr;
    }

    return promiseObj;
}

// thread safe function async interface
static napi_value GetImagePathAsyncTSF(napi_env env, napi_callback_info info) {
    size_t paraNum = 2;
    napi_value paraArray[2] = {nullptr};

    // parse parameters
    napi_status operStatus = napi_get_cb_info(env, info, ?Num, paraArray, nullptr, nullptr);
    if (operStatus != napi_ok) {
        return nullptr;
    }

    napi_valuetype paraDataType = napi_undefined;
    operStatus = napi_typeof(env, paraArray[0], ?DataType);
    if ((operStatus != napi_ok) || (paraDataType != napi_string)) {
        return nullptr;
    }

    operStatus = napi_typeof(env, paraArray[1], ?DataType);
    if ((operStatus != napi_ok) || (paraDataType != napi_function)) {
        return nullptr;
    }

    // napi_value convert to char *
    constexpr size_t buffSize = 100;
    char strBuff[buffSize]{}; // char buffer for imageName string
    size_t strLength = 0;
    operStatus = napi_get_value_string_utf8(env, paraArray[0], strBuff, buffSize, &strLength);
    if ((operStatus != napi_ok) || (strLength == 0)) {
        return nullptr;
    }

    // async resource
    napi_value asyncName = nullptr;
    string asyncStr = "async napi_threadsafe_function";
    operStatus = napi_create_string_utf8(env, asyncStr.c_str(), asyncStr.length(), &asyncName);
    if (operStatus != napi_ok) {
        return nullptr;
    }

    // defines context data. the memory will be released in CompleteFunc
    auto contextData = new ContextData;
    contextData->args = strBuff;

    // create thread safe function
    if (tsFun == nullptr) {
        operStatus =
            napi_create_threadsafe_function(env, paraArray[1], nullptr, asyncName, MAX_MSG_QUEUE_SIZE,
                                            INITIAL_THREAD_COUNT, nullptr, nullptr, nullptr, CallJsFunction, &tsFun);
        if (operStatus != napi_ok) {
            DeleteContext(env, contextData);
            return nullptr;
        }
    }

    // create producer thread
    thread producer(ProductElement, static_cast<void *>(contextData));
    producer.detach(); // must be detached

    // create consumer thread
    thread consumer(ConsumeElementTSF, static_cast<void *>(contextData));
    consumer.detach();

    return nullptr;
}

EXTERN_C_START
static napi_value Init(napi_env env, napi_value exports) {
    napi_property_descriptor desc[] = {
        {"getImagePathSync", nullptr, GetImagePathSync, nullptr, nullptr, nullptr, napi_default, nullptr},
        {"getImagePathAsyncCallBack", nullptr, GetImagePathAsyncCallBack, nullptr, nullptr, nullptr, napi_default,
         nullptr},
        {"getImagePathAsyncPromise", nullptr, GetImagePathAsyncPromise, nullptr, nullptr, nullptr, napi_default,
         nullptr},
        {"getImagePathAsyncTSF", nullptr, GetImagePathAsyncTSF, nullptr, nullptr, nullptr, napi_default, nullptr}};
    napi_define_properties(env, exports, sizeof(desc) / sizeof(desc[0]), desc);
    return exports;
}
EXTERN_C_END

static napi_module demoModule = {
    .nm_version = 1,
    .nm_flags = 0,
    .nm_filename = nullptr,
    .nm_register_func = Init,
    .nm_modname = "entry",
    .nm_priv = ((void *)0),
    .reserved = {0},
};

extern "C" __attribute__((constructor)) void RegisterEntryModule(void) { napi_module_register(&demoModule); }

3. 生產(chǎn)者線程執(zhí)行函數(shù)

生產(chǎn)者線程的執(zhí)行功能:通過(guò)解析上下文數(shù)據(jù)中的圖片名稱(chēng)參數(shù),來(lái)搜索相應(yīng)的圖片路徑,并將其置入模型緩沖隊(duì)列中。

MultiThreads.cpp文件

static void ProductElement(void *data) {
    buffQueue.PutElement(SearchImagePath(static_cast<ContextData *>(data)->args));
}

4. 消費(fèi)者線程執(zhí)行函數(shù)

消費(fèi)者線程的執(zhí)行功能:通過(guò)從模型緩沖隊(duì)列中獲取圖片路徑的搜索結(jié)果,并將其置入上下文數(shù)據(jù)中,用以后續(xù)反饋給ArkTS應(yīng)用側(cè)。

MultiThreads.cpp文件

非線程安全函數(shù)消費(fèi)者執(zhí)行函數(shù):

static void ConsumeElement(void *data) { static_cast<ContextData *>(data)->result = buffQueue.TakeElement(); }

線程安全函數(shù)消費(fèi)者執(zhí)行函數(shù):

static void ConsumeElementTSF(void *data) {
    static_cast<ContextData *>(data)->result = buffQueue.TakeElement();
    // bind consumer thread to the thread safe function
    (void)napi_acquire_threadsafe_function(tsFun);
    // send async task to the JS main thread EventLoop
    (void)napi_call_threadsafe_function(tsFun, data, napi_tsfn_blocking);
    // release thread reference
    (void)napi_release_threadsafe_function(tsFun, napi_tsfn_release);
}

四、使用Node-API進(jìn)行同步任務(wù)開(kāi)發(fā)

在同步任務(wù)開(kāi)發(fā)中,ArkTS應(yīng)用側(cè)主線程將阻塞等待Native側(cè)計(jì)算結(jié)果,Native側(cè)計(jì)算結(jié)果通過(guò)方舟引擎反饋給ArkTS應(yīng)用側(cè)。此過(guò)程中,Native接口代碼與ArkTS應(yīng)用側(cè)均運(yùn)行在ArkTS主線程上。為了Native側(cè)業(yè)務(wù)處理具有同步效果,案例中的生產(chǎn)者與消費(fèi)者線程則采用join()的方式來(lái)進(jìn)行同步處理。
同步調(diào)用也支持帶Callback,具體方式由應(yīng)用開(kāi)發(fā)者決定,通過(guò)是否傳遞Callback函數(shù)進(jìn)行區(qū)分。

同步任務(wù)開(kāi)發(fā)時(shí)序交互圖

從上圖中,可以看出,當(dāng)Native同步任務(wù)接口被調(diào)用時(shí),該接口會(huì)完成參數(shù)解析、數(shù)據(jù)類(lèi)型轉(zhuǎn)換、創(chuàng)建生產(chǎn)者和消費(fèi)者線程等。在等待生產(chǎn)者線程和消費(fèi)者線程執(zhí)行結(jié)束后,將圖片路徑結(jié)果轉(zhuǎn)換為napi_value,由方舟引擎直接反饋到ArkTS應(yīng)用側(cè)。

4.1 同步任務(wù)開(kāi)發(fā)步驟

4.1.1 ArkTS應(yīng)用側(cè)開(kāi)發(fā)

用戶(hù)在界面點(diǎn)擊“多線程同步調(diào)用”按鈕,觸發(fā)onClick()回調(diào)。在回調(diào)處理中,將會(huì)調(diào)用Native接口進(jìn)行圖片路徑獲取,以刷新UI界面。

Index.ets文件

import testNapi from 'libentry.so';
import Constants from '../../common/constants/CommonConstants';


@Entry
@Component
struct Index {
  @State imagePath: string = Constants.INIT_IMAGE_PATH;
  imageName: string = '';


  build() {
    Column() {
      ...
      // button list, prompting the user to click the button to select the target image.
      Column() {
        ...
        // multi-threads sync call button
        Button($r('app.string.sync_button_title'))
          .width(Constants.FULL_PARENT)
          .margin($r('app.float.button_common_margin'))
          .onClick(() => {
            this.imageName = Constants.SYNC_BUTTON_IMAGE;
            this.imagePath = Constants.IMAGE_ROOT_PATH + testNapi.getImagePathSync(this.imageName);
          })
      ...
      }
      ...
    }
    ...
  }
}
4.1.2 Native側(cè)開(kāi)發(fā)

在同步任務(wù)開(kāi)發(fā)中,Native側(cè)接口原生代碼仍然運(yùn)行在ArkTS主線程上。
導(dǎo)出Native接口:將Native接口導(dǎo)出到ArkTS側(cè),用于支撐ArkTS對(duì)象調(diào)用和模塊編譯構(gòu)建。

index.d.ts文件

// export sync interface
export const getImagePathSync: (imageName: string) => string;

上下文數(shù)據(jù)結(jié)構(gòu)定義:用于任務(wù)執(zhí)行過(guò)程中,上下文數(shù)據(jù)存儲(chǔ)。后文異步任務(wù)開(kāi)發(fā)和線程安全開(kāi)發(fā)中實(shí)現(xiàn)相同,后續(xù)不再贅述。

MultiThreads.cpp文件

// context data provided by users
// data is transferred between the native method (initialization data), ExecuteFunc, and CompleteFunc
struct ContextData {
    napi_async_work asyncWork = nullptr; // async work object
    napi_deferred deferred = nullptr;    // associated object of the delay object promise
    napi_ref callbackRef = nullptr;      // reference of callback
    string args = "";                    // parameters from ArkTS --- imageName
    string result = "";                  // C++ sub-thread calculation result --- imagePath
};

銷(xiāo)毀上下文數(shù)據(jù):在任務(wù)執(zhí)行結(jié)束或者失敗后,需要銷(xiāo)毀上下文數(shù)據(jù)進(jìn)行內(nèi)存釋放。后文異步任務(wù)開(kāi)發(fā)和線程安全開(kāi)發(fā)中實(shí)現(xiàn)相同,后續(xù)不再贅述。

MultiThreads.cpp文件

static void DeleteContext(napi_env env, ContextData *contextData) {
    // delete callback reference
    if (contextData->callbackRef != nullptr) {
        (void)napi_delete_reference(env, contextData->callbackRef);
    }
    // delete async work
    if (contextData->asyncWork != nullptr) {
        (void)napi_delete_async_work(env, contextData->asyncWork);
    }
    // release context data
    delete contextData;
}

Native同步任務(wù)開(kāi)發(fā)接口:解析ArkTS應(yīng)用側(cè)參數(shù)、數(shù)據(jù)類(lèi)型轉(zhuǎn)換、創(chuàng)建生產(chǎn)者和消費(fèi)者線程,并使用join()進(jìn)行同步處理。最后在獲取結(jié)果后,將數(shù)據(jù)轉(zhuǎn)換為napi_value類(lèi)型,返回給ArkTS應(yīng)用側(cè)。

MultiThreads.cpp文件

// sync interface
static napi_value GetImagePathSync(napi_env env, napi_callback_info info) {
    size_t paraNum = 1;
    napi_value paraArray[1] = {nullptr};
    // parse parameters
    napi_status operStatus = napi_get_cb_info(env, info, ?Num, paraArray, nullptr, nullptr);
    if (operStatus != napi_ok) {
        return nullptr;
    }
    napi_valuetype paraDataType = napi_undefined;
    operStatus = napi_typeof(env, paraArray[0], ?DataType);
    if ((operStatus != napi_ok) || (paraDataType != napi_string)) {
        return nullptr;
    }
    // convert napi_value to char *
    constexpr size_t buffSize = 100;
    char strBuff[buffSize]{}; // char buffer for imageName string
    size_t strLength = 0;
    operStatus = napi_get_value_string_utf8(env, paraArray[0], strBuff, buffSize, &strLength);
    if ((operStatus != napi_ok) || (strLength == 0)) {
        return nullptr;
    }
    // defines context data. the memory will be released in CompleteFunc
    auto contextData = new ContextData;
    contextData->args = strBuff;
    // create producer thread
    thread producer(ProductElement, static_cast<void *>(contextData));
    producer.join();
    // create consumer thread
    thread consumer(ConsumeElement, static_cast<void *>(contextData));
    consumer.join();
    // convert the result to napi_value and send it to ArkTs application
    napi_value result = nullptr;
    (void)napi_create_string_utf8(env, contextData->result.c_str(), contextData->result.length(), &result);
    // delete context data
    DeleteContext(env, contextData);
    return result;
}

五、使用Node-API進(jìn)行異步任務(wù)開(kāi)發(fā)

5.1 Node-API異步任務(wù)機(jī)制概述

Node-API異步任務(wù)開(kāi)發(fā)主要用于執(zhí)行耗時(shí)操作的場(chǎng)景中使用,以避免阻塞主線程,確保應(yīng)用程序的性能和響應(yīng)效率。例如以下場(chǎng)景:

  • 文件操作:讀取大型文件或執(zhí)行復(fù)雜的文件操作時(shí),可以使用異步工作項(xiàng)來(lái)避免阻塞主線程。
  • 網(wǎng)絡(luò)請(qǐng)求:當(dāng)需要進(jìn)行網(wǎng)絡(luò)請(qǐng)求并等待響應(yīng)時(shí),可以使用異步工作項(xiàng)來(lái)避免阻塞主線程,從而提高應(yīng)用程序的響應(yīng)性能。
  • 數(shù)據(jù)庫(kù)操作:當(dāng)需要執(zhí)行復(fù)雜的數(shù)據(jù)庫(kù)查詢(xún)或?qū)懭氩僮鲿r(shí),可以使用異步工作項(xiàng)來(lái)避免阻塞主線程,從而提高應(yīng)用程序的并發(fā)性能。
  • 圖形處理:當(dāng)需要對(duì)大型圖像進(jìn)行處理或執(zhí)行復(fù)雜的圖像算法時(shí),可以使用異步工作項(xiàng)來(lái)避免阻塞主線程,從而提高應(yīng)用程序的實(shí)時(shí)性能。

異步方式與同步方式的區(qū)別在于,同步方式中所有代碼的處理都在ArkTS主線程中完成,而異步方式中的所有代碼在多線程中完成。Node-API主要是通過(guò)創(chuàng)建一個(gè)異步工作項(xiàng)來(lái)實(shí)現(xiàn)異步任務(wù)開(kāi)發(fā)??傮w步驟如下:

  1. 在Native接口函數(shù)中,創(chuàng)建一個(gè)異步工作項(xiàng),并置入libuv調(diào)度隊(duì)列中,然后立即返回一個(gè)臨時(shí)結(jié)果給ArkTS調(diào)用者;
  2. 通過(guò)libuv線程池創(chuàng)建并調(diào)度work子線程完成異步業(yè)務(wù)邏輯的執(zhí)行;
  3. 通過(guò)Callback回調(diào)或者Promise延時(shí)對(duì)象返回真正的處理結(jié)果,并用于應(yīng)用側(cè)UI刷新。

異步工作項(xiàng)的底層機(jī)制是基于libuv異步庫(kù)來(lái)實(shí)現(xiàn)的,具體流程原理如下:

依賴(lài)Node-API提供的napi_create_async_work接口創(chuàng)建異步工作項(xiàng):

NAPI_EXTERN napi_status napi_create_async_work(napi_env env,
                                               napi_value async_resource,
                                               napi_value async_resource_name,
                                               napi_async_execute_callback execute,
                                               napi_async_complete_callback complete,
                                               void* data,
                                               napi_async_work* result);
參數(shù)說(shuō)明:
[in] env:傳入接口調(diào)用者的環(huán)境,包含方舟引擎等。由框架提供,默認(rèn)情況下直接傳入即可。
[in] async_resource:可選項(xiàng),關(guān)聯(lián)async_hooks。
[in] async_resource_name:異步資源標(biāo)識(shí)符,主要用于async_hooks API暴露斷言診斷信息。
[in] execute:執(zhí)行業(yè)務(wù)邏輯計(jì)算函數(shù),由libuv線程池調(diào)度執(zhí)行。在該函數(shù)中執(zhí)行IO、CPU密集型任務(wù),不阻塞主線程。
[in] complete:execute回調(diào)函數(shù)執(zhí)行完成或取消后,觸發(fā)執(zhí)行該函數(shù)。此函數(shù)在EventLoop子線程中執(zhí)行。
[in] data:用戶(hù)提供的上下文數(shù)據(jù),用于傳遞數(shù)據(jù)。
[out] result:napi_async_work*指針,用于返回當(dāng)前此處函數(shù)調(diào)用創(chuàng)建的異步工作項(xiàng)。 返回值:返回napi_ok表示轉(zhuǎn)換成功,其他值失敗。

Execute回調(diào)

  • execute函數(shù)用于執(zhí)行工作項(xiàng)的業(yè)務(wù)邏輯,異步工作項(xiàng)被調(diào)度后,該函數(shù)從上下文數(shù)據(jù)中獲取輸入數(shù)據(jù),在work子線程中完成業(yè)務(wù)邏輯計(jì)算(不阻塞主線程)并將結(jié)果寫(xiě)入上下文數(shù)據(jù)。
  • 因?yàn)閑xecute函數(shù)不在ArkTS線程中,所以不允許execute函數(shù)調(diào)用napi的接口。業(yè)務(wù)邏輯的返回值可以返回到complete回調(diào)中處理。

Complete回調(diào)

  • 業(yè)務(wù)邏輯處理execute函數(shù)執(zhí)行完成或被取消后,通過(guò)事件通知EventLoop執(zhí)行complete函數(shù),complete函數(shù)從上下文數(shù)據(jù)中獲取結(jié)果,轉(zhuǎn)換為napi_value類(lèi)型,調(diào)用ArkTS回調(diào)函數(shù)或通過(guò)Promise resolve()返回結(jié)果。
  • 該函數(shù)運(yùn)行在ArkTS主線程下,因此可以調(diào)用napi的接口,將execute中的返回值封裝成ArkTS對(duì)象返回。

Node-API異步接口實(shí)現(xiàn)支持Callback方式和Promise方式,具體使用哪種方式由應(yīng)用開(kāi)發(fā)者決定,通過(guò)是否傳遞callback函數(shù)進(jìn)行區(qū)分。下文將對(duì)兩種異步模型作簡(jiǎn)要介紹:

Callback異步模型

  • 用戶(hù)在調(diào)用Native接口的時(shí)候,Native接口將異步執(zhí)行任務(wù),并臨時(shí)返回空值給ArkTS應(yīng)用側(cè)。
  • 異步任務(wù)執(zhí)行結(jié)果以參數(shù)的形式提供給用戶(hù)注冊(cè)的ArkTS回調(diào)函數(shù),并通過(guò)napi_call_function將ArkTS回調(diào)函數(shù)進(jìn)行調(diào)用執(zhí)行以反饋結(jié)果到ArkTS應(yīng)用側(cè)。

Promise異步模型

  • 用戶(hù)在調(diào)用Native接口的時(shí)候,Native接口將異步執(zhí)行任務(wù),并返回一個(gè)Promise對(duì)象給ArkTS應(yīng)用側(cè)。
  • Promise對(duì)象提供了API使得異步執(zhí)行可以按照同步的流程表示出來(lái),避免了層層嵌套的回調(diào)引用。
  • 異步任務(wù)執(zhí)行結(jié)果以參數(shù)的形式提供給與ArkTS應(yīng)用側(cè)Promise對(duì)象關(guān)聯(lián)的deferred對(duì)象,并通過(guò)napi_resolve_deferred將計(jì)算結(jié)果反饋到ArkTS應(yīng)用側(cè)。

5.2 異步任務(wù)開(kāi)發(fā)時(shí)序交互圖

Callback異步開(kāi)發(fā)時(shí)序交互

Promise異步開(kāi)發(fā)時(shí)序交互

從上圖中,可以看出,當(dāng)Native異步任務(wù)接口被調(diào)用時(shí),該接口會(huì)完成參數(shù)解析、數(shù)據(jù)類(lèi)型轉(zhuǎn)換、異步工作項(xiàng)創(chuàng)建、異步任務(wù)壓入調(diào)度隊(duì)列以及返回空值(Callback方式)或者Promise對(duì)象(Promise方式)等。異步任務(wù)的調(diào)度、執(zhí)行以及反饋計(jì)算結(jié)果,均由libuv線程池和EventLoop機(jī)制進(jìn)行系統(tǒng)調(diào)度完成。

六、異步任務(wù)開(kāi)發(fā)步驟

6.1 Callback異步開(kāi)發(fā)步驟

6.1.1 ArkTS應(yīng)用側(cè)開(kāi)發(fā)

用戶(hù)在界面點(diǎn)擊“多線程callback異步調(diào)用”按鈕,觸發(fā)onClick()回調(diào)。在回調(diào)處理中,將會(huì)調(diào)用Native接口進(jìn)行圖片路徑獲取,以刷新UI界面。

Index.ets文件

import testNapi from 'libentry.so';
import Constants from '../../common/constants/CommonConstants';


@Entry
@Component
struct Index {
  @State imagePath: string = Constants.INIT_IMAGE_PATH;
  imageName: string = '';


  build() {
    Column() {
      ...
      // button list, prompting the user to click the button to select the target image.
      Column() {
        ...
        // multi-threads callback async button
        Button($r('app.string.async_callback_button_title'))
          .width(Constants.FULL_PARENT)
          .margin($r('app.float.button_common_margin'))
          .onClick(() => {
            this.imageName = Constants.CALLBACK_BUTTON_IMAGE;
            testNapi.getImagePathAsyncCallBack(this.imageName, (result: string) => {
              this.imagePath = Constants.IMAGE_ROOT_PATH + result;
            });
          })
      ...
      }
      ...
    }
    ...
  }
}
6.1.2 Native側(cè)開(kāi)發(fā)

導(dǎo)出Native接口: 將Native接口導(dǎo)出到ArkTS側(cè),用于支撐ArkTS對(duì)象調(diào)用和模塊編譯構(gòu)建。

index.d.ts文件

// export async callback interface
export const getImagePathAsyncCallBack: (imageName: string, callBack: (result: string) => void) => void;

execute回調(diào):定義異步工作項(xiàng)的第一個(gè)回調(diào)函數(shù),該函數(shù)在work子線程中執(zhí)行,處理具體的業(yè)務(wù)邏輯。

MultiThreads.cpp文件

static void ExecuteFunc([[maybe_unused]] napi_env env, void *data) {
    // create producer thread
    thread producer(ProductElement, data);
    // the producer and consumer threads must be synchronized
    // otherwise, the complete operation is triggered to communicate with the ArkTS after the executeFunc is complete
    // the result is unpredictable
    producer.join();
    // create consumer thread
    thread consumer(ConsumeElement, data);
    consumer.join();
}

complete回調(diào): 定義異步工作項(xiàng)的第二個(gè)回調(diào)函數(shù),該函數(shù)在ArkTS主線程中執(zhí)行,將結(jié)果傳遞給ArkTS側(cè)。

MultiThreads.cpp文件

static void CompleteFuncCallBack(napi_env env, [[maybe_unused]] napi_status status, void *data) {
    // parse context data
    ContextData *contextData = static_cast<ContextData *>(data);
    napi_value callBack = nullptr;
    napi_status operStatus = napi_get_reference_value(env, contextData->callbackRef, &callBack);
    if (operStatus != napi_ok) {
        DeleteContext(env, contextData);
        return;
    }
    // define the undefined variable, which is used in napi_call_function
    // because no other data is transferred, the variable is defined as undefined
    napi_value undefined = nullptr;
    operStatus = napi_get_undefined(env, &undefined);
    if (operStatus != napi_ok) {
        DeleteContext(env, contextData);
        return;
    }
    // convert the calculation result of C++ sub-thread to the napi_value type
    napi_value callBackArgs = nullptr;
    operStatus = napi_create_string_utf8(env, contextData->result.c_str(),
        contextData->result.length(), &callBackArgs);
    if (operStatus != napi_ok) {
        DeleteContext(env, contextData);
        return;
    }
    // call the JS callback and send the async calculation result on the Native to ArkTS application
    napi_value callBackResult = nullptr;
    (void)napi_call_function(env, undefined, callBack, 1, &callBackArgs, &callBackResult);
    // destroy data and release memory
    DeleteContext(env, contextData);
}

Native異步任務(wù)開(kāi)發(fā)接口:解析ArkTS應(yīng)用側(cè)參數(shù),使用napi_create_async_work創(chuàng)建異步工作項(xiàng),并使用napi_queue_async_work將異步任務(wù)加入隊(duì)列,等待調(diào)度執(zhí)行。

MultiThreads.cpp文件

// callback async interface
static napi_value GetImagePathAsyncCallBack(napi_env env, napi_callback_info info) {
    size_t paraNum = 2;
    napi_value paraArray[2] = {nullptr};
    // parse parameters
    napi_status operStatus = napi_get_cb_info(env, info, ?Num, paraArray, nullptr, nullptr);
    if (operStatus != napi_ok) {
        return nullptr;
    }
    napi_valuetype paraDataType = napi_undefined;
    operStatus = napi_typeof(env, paraArray[0], ?DataType);
    if ((operStatus != napi_ok) || (paraDataType != napi_string)) {
        return nullptr;
    }
    operStatus = napi_typeof(env, paraArray[1], ?DataType);
    if ((operStatus != napi_ok) || (paraDataType != napi_function)) {
        return nullptr;
    }
    // napi_value convert to char *
    constexpr size_t buffSize = 100;
    char strBuff[buffSize]{}; // char buffer for imageName string
    size_t strLength = 0;
    operStatus = napi_get_value_string_utf8(env, paraArray[0], strBuff, buffSize, &strLength);
    if ((operStatus != napi_ok) || (strLength == 0)) {
        return nullptr;
    }
    // defines context data. the memory will be released in CompleteFunc
    auto contextData = new ContextData;
    contextData->args = strBuff;
    operStatus = napi_create_reference(env, paraArray[1], 1, &contextData->callbackRef);
    if (operStatus != napi_ok) {
        DeleteContext(env, contextData);
        return nullptr;
    }
    // async resource
    napi_value asyncName = nullptr;
    string asyncStr = "async callback";
    operStatus = napi_create_string_utf8(env, asyncStr.c_str(), asyncStr.length(), &asyncName);
    if (operStatus != napi_ok) {
        DeleteContext(env, contextData);
        return nullptr;
    }
    // create async work
    operStatus = napi_create_async_work(env, nullptr, asyncName, ExecuteFunc, CompleteFuncCallBack,
                                        static_cast<void *>(contextData), &contextData->asyncWork);
    if (operStatus != napi_ok) {
        DeleteContext(env, contextData);
        return nullptr;
    }
    // add the async work to the queue and wait for scheduling
    operStatus = napi_queue_async_work(env, contextData->asyncWork);
    if (operStatus != napi_ok) {
        DeleteContext(env, contextData);
    }
    return nullptr;
}

6.2 Promise異步開(kāi)發(fā)步驟

6.2.1 ArkTS應(yīng)用側(cè)開(kāi)發(fā)

用戶(hù)在界面點(diǎn)擊“多線程promise異步調(diào)用”按鈕,觸發(fā)onClick()回調(diào)。在回調(diào)處理中,將會(huì)調(diào)用Native接口進(jìn)行圖片路徑獲取,以刷新UI界面。

Index.ets文件

import testNapi from 'libentry.so';
import Constants from '../../common/constants/CommonConstants';


@Entry
@Component
struct Index {
  @State imagePath: string = Constants.INIT_IMAGE_PATH;
  imageName: string = '';


  build() {
    Column() {
      ...
      // button list, prompting the user to click the button to select the target image.
      Column() {
        ...
        // multi-threads promise async button
        Button($r('app.string.async_promise_button_title'))
          .width(Constants.FULL_PARENT)
          .margin($r('app.float.button_common_margin'))
          .onClick(() => {
            this.imageName = Constants.PROMISE_BUTTON_IMAGE;
            let promiseObj = testNapi.getImagePathAsyncPromise(this.imageName);
            promiseObj.then((result: string) => {
              this.imagePath = Constants.IMAGE_ROOT_PATH + result;
            })
          })
      ...
      }
      ...
    }
    ...
  }
}
6.2.2 Native側(cè)開(kāi)發(fā)

導(dǎo)出Native接口:將Native接口導(dǎo)出到ArkTS側(cè),用于支撐ArkTS對(duì)象調(diào)用和模塊編譯構(gòu)建。

index.d.ts文件

// export async promise interface
export const getImagePathAsyncPromise: (imageName: string) => Promise<string>;

execute回調(diào):定義異步工作項(xiàng)的第一個(gè)回調(diào)函數(shù),該函數(shù)在work子線程中執(zhí)行,處理具體的業(yè)務(wù)邏輯。

MultiThreads.cpp文件

static void ExecuteFunc([[maybe_unused]] napi_env env, void *data) {
    // create producer thread
    thread producer(ProductElement, data);
    // the producer and consumer threads must be synchronized
    // otherwise, the complete operation is triggered to communicate with the ArkTS after the executeFunc is complete
    // the result is unpredictable
    producer.join();
    // create consumer thread
    thread consumer(ConsumeElement, data);
    consumer.join();
}

complete回調(diào):定義異步工作項(xiàng)的第二個(gè)回調(diào)函數(shù),該函數(shù)在ArkTS主線程中執(zhí)行,將結(jié)果傳遞給ArkTS側(cè)。

MultiThreads.cpp文件

static void CompleteFuncPromise(napi_env env, [[maybe_unused]] napi_status status, void *data) {
    // parse context data
    ContextData *contextData = static_cast<ContextData *>(data);
    // convert the calculation result of C++ sub-thread to the napi_value type
    napi_value promiseArgs = nullptr;
    napi_status operStatus =
        napi_create_string_utf8(env, contextData->result.c_str(), contextData->result.length(), &promiseArgs);
    if (operStatus != napi_ok) {
        DeleteContext(env, contextData);
        return;
    }
    // the deferred and promise object are associated. the result is sent to ArkTS application through this interface
    operStatus = napi_resolve_deferred(env, contextData->deferred, promiseArgs);
    if (operStatus != napi_ok) {
        DeleteContext(env, contextData);
        return;
    }
    // destroy data and release memory
    DeleteContext(env, contextData);
}

Native異步任務(wù)開(kāi)發(fā)接口:解析ArkTS應(yīng)用側(cè)參數(shù),使用napi_create_async_work創(chuàng)建異步工作項(xiàng),并使用napi_queue_async_work將異步任務(wù)加入隊(duì)列,等待調(diào)度執(zhí)行。

MultiThreads.cpp文件

// promise async interface
static napi_value GetImagePathAsyncPromise(napi_env env, napi_callback_info info) {
    size_t paraNum = 1;
    napi_value paraArray[1] = {nullptr};
    // parse parameters
    napi_status operStatus = napi_get_cb_info(env, info, ?Num, paraArray, nullptr, nullptr);
    if (operStatus != napi_ok) {
        return nullptr;
    }
    napi_valuetype paraDataType = napi_undefined;
    operStatus = napi_typeof(env, paraArray[0], ?DataType);
    if ((operStatus != napi_ok) || (paraDataType != napi_string)) {
        return nullptr;
    }
    // napi_value convert to char *
    constexpr size_t buffSize = 100;
    char strBuff[buffSize]{}; // char buffer for imageName string
    size_t strLength = 0;
    operStatus = napi_get_value_string_utf8(env, paraArray[0], strBuff, buffSize, &strLength);
    if ((operStatus != napi_ok) || (strLength == 0)) {
        return nullptr;
    }
    // defines context data. the memory will be released in CompleteFunc
    auto contextData = new ContextData;
    contextData->args = strBuff;
    // async resource
    napi_value asyncName = nullptr;
    string asyncStr = "async promise";
    operStatus = napi_create_string_utf8(env, asyncStr.c_str(), asyncStr.length(), &asyncName);
    if (operStatus != napi_ok) {
        DeleteContext(env, contextData);
        return nullptr;
    }
    // create async work
    operStatus = napi_create_async_work(env, nullptr, asyncName, ExecuteFunc, CompleteFuncPromise,
                                        static_cast<void *>(contextData), &contextData->asyncWork);
    if (operStatus != napi_ok) {
        DeleteContext(env, contextData);
        return nullptr;
    }
    // add the async work to the queue and wait for scheduling
    operStatus = napi_queue_async_work(env, contextData->asyncWork);
    if (operStatus != napi_ok) {
        DeleteContext(env, contextData);
        return nullptr;
    }
    // create promise object
    napi_value promiseObj = nullptr;
    operStatus = napi_create_promise(env, &contextData->deferred, &promiseObj);
    if (operStatus != napi_ok) {
        DeleteContext(env, contextData);
        return nullptr;
    }
    return promiseObj;
}

七、使用Node-API進(jìn)行線程安全開(kāi)發(fā)

7.1 Node-API線程安全機(jī)制概述

Node-API線程安全開(kāi)發(fā)主要用于異步多線程之間共享和調(diào)用場(chǎng)景中使用,以避免出現(xiàn)競(jìng)爭(zhēng)條件或死鎖。例如以下場(chǎng)景:

  • 異步計(jì)算:如果需要進(jìn)行耗時(shí)的計(jì)算或IO操作,可以創(chuàng)建一個(gè)線程安全函數(shù),將計(jì)算或IO操作放在另一個(gè)線程中執(zhí)行,避免阻塞主線程,提高程序的響應(yīng)速度。
  • 數(shù)據(jù)共享:如果多個(gè)線程需要訪問(wèn)同一份數(shù)據(jù),可以創(chuàng)建一個(gè)線程安全函數(shù),確保數(shù)據(jù)的讀寫(xiě)操作不會(huì)發(fā)生競(jìng)爭(zhēng)條件或死鎖等問(wèn)題。
  • 多線程開(kāi)發(fā):如果需要進(jìn)行多線程開(kāi)發(fā),可以創(chuàng)建一個(gè)線程安全函數(shù),確保多個(gè)線程之間的通信和同步操作正確無(wú)誤。

Node-API接口只能在ArkTS主線程上進(jìn)行調(diào)用。當(dāng)C++子線程或者work子線程需要調(diào)用ArkTS回調(diào)接口或者Node-API接口時(shí),這些線程是需要與ArkTS主線程進(jìn)行通信才能完成的。Node-API提供了類(lèi)型napi_threadsafe_function(線程安全函數(shù))以及創(chuàng)建、銷(xiāo)毀和調(diào)用該類(lèi)型對(duì)象的 API來(lái)完成此操作。Node-API主要是通過(guò)創(chuàng)建一個(gè)線程安全函數(shù),然后在C++子線程或者work子線程中調(diào)用線程安全函數(shù)來(lái)實(shí)現(xiàn)線程安全開(kāi)發(fā)。總體步驟如下:

  1. 在Native接口函數(shù)中,創(chuàng)建一個(gè)線程安全函數(shù)對(duì)象,并注冊(cè)綁定ArkTS回調(diào)接口callback和線程安全回調(diào)函數(shù)call_js_cb,然后立即返回一個(gè)臨時(shí)結(jié)果給ArkTS調(diào)用者;
  2. 通過(guò)系統(tǒng)調(diào)度C++子線程完成異步業(yè)務(wù)邏輯的執(zhí)行,并在子線程的執(zhí)行函數(shù)中調(diào)用napi_call_threadsafe_function,將call_js_cb拋給EventLoop事件循環(huán)進(jìn)行調(diào)度;
  3. 通過(guò)call_js_cb執(zhí)行,調(diào)用napi_call_function調(diào)用ArkTS回調(diào)接口callback,從而將異步計(jì)算結(jié)果反饋到ArkTS應(yīng)用側(cè),用于應(yīng)用側(cè)UI刷新。

線程安全函數(shù)的底層機(jī)制依然是基于libuv異步庫(kù)來(lái)實(shí)現(xiàn)的,其原理流程如下:

線程安全函數(shù)機(jī)制中關(guān)鍵的兩個(gè)接口napi_create_threadsafe_function()和napi_call_threadsafe_function()參數(shù)列表詳解:

napi_create_threadsafe_function

該接口主要用于創(chuàng)建線程安全對(duì)象,在創(chuàng)建的過(guò)程中,會(huì)注冊(cè)異步過(guò)程中的關(guān)鍵信息:ArkTS側(cè)回調(diào)接口callback、線程安全回調(diào)函數(shù)call_js_cb等。

NAPI_EXTERN napi_status napi_create_threadsafe_function(napi_env env,
                            napi_value func,
                            napi_value async_resource,
                            napi_value async_resource_name,
                            size_t max_queue_size,
                            size_t initial_thread_count,
                            void* thread_finalize_data,
                            napi_finalize thread_finalize_cb,
                            void* context,
                            napi_threadsafe_function_call_js call_js_cb,
                            napi_threadsafe_function* result);
參數(shù)說(shuō)明:
[in] env:傳入接口調(diào)用者的環(huán)境,包含方舟引擎等。由框架提供,默認(rèn)情況下直接傳入即可。
[in] func:ArkTS應(yīng)用側(cè)傳入的回調(diào)接口callback,可為空。當(dāng)該值為nullptr時(shí),下文call_js_cb不能為nullptr。反之,亦然。兩者不可同時(shí)為空。
[in] async_resource:關(guān)聯(lián)async_hooks,可為空。
[in] async_resource_name:異步資源標(biāo)識(shí)符,主要用于async_hooks API暴露斷言診斷信息。
[in] max_queue_size:緩沖隊(duì)列容量,0表示無(wú)限制。線程安全函數(shù)實(shí)現(xiàn)實(shí)質(zhì)為生產(chǎn)者-消費(fèi)者模型。
[in] initial_thread_count:初始線程,包括將使用此函數(shù)的主線程,可為空。
[in] thread_finalize_data:傳遞給thread_finalize_cb接口的參數(shù),可為空。
[in] thread_finalize_cb:當(dāng)線程安全函數(shù)結(jié)束釋放時(shí)的回調(diào)接口,可為空。
[in] context:附加的上下文數(shù)據(jù),可為空。
[in] call_js_cb:子線程需要處理的線程安全回調(diào)任務(wù),類(lèi)似于異步工作項(xiàng)中的complete回調(diào)。當(dāng)調(diào)用napi_call_threadsafe_function后,被拋到ArkTS主線程EventLoop中,等待調(diào)度執(zhí)行。當(dāng)該值為空時(shí),系統(tǒng)將會(huì)調(diào)用默認(rèn)回調(diào)接口。
[out] result:線程安全函數(shù)對(duì)象指針。

napi_call_threadsafe_function

該接口主要用于子線程中,將需要回到ArkTS主線程處理的任務(wù)拋到EventLoop中,等待調(diào)度執(zhí)行。

線程安全開(kāi)發(fā)時(shí)序交互圖

從上圖中,可以看出,當(dāng)Native線程安全異步接口被調(diào)用時(shí),該接口會(huì)完成參數(shù)解析、數(shù)據(jù)類(lèi)型轉(zhuǎn)換、線程安全創(chuàng)建、在創(chuàng)建過(guò)程中的注冊(cè)綁定ArkTS回調(diào)處理函數(shù)以及創(chuàng)建生產(chǎn)者和消費(fèi)者線程等。異步任務(wù)的調(diào)度、執(zhí)行以及反饋計(jì)算結(jié)果,均由C++子線程和EventLoop機(jī)制進(jìn)行系統(tǒng)調(diào)度完成。

7.2 線程安全開(kāi)發(fā)步驟

7.2.1 ArkTS應(yīng)用側(cè)開(kāi)發(fā)

用戶(hù)在界面點(diǎn)擊“多線程tsf異步調(diào)用”按鈕,觸發(fā)onClick()回調(diào)。在回調(diào)處理中,將會(huì)調(diào)用Native接口進(jìn)行圖片路徑獲取,以刷新UI界面。

Index.ets文件

import testNapi from 'libentry.so';
import Constants from '../../common/constants/CommonConstants';


@Entry
@Component
struct Index {
  @State imagePath: string = Constants.INIT_IMAGE_PATH;
  imageName: string = '';


  build() {
    Column() {
      ...
      // button list, prompting the user to click the button to select the target image.
      Column() {
        ...
        // multi-threads tsf async button
        Button($r('app.string.async_tsf_button_title'))
          .width(Constants.FULL_PARENT)
          .margin($r('app.float.button_common_margin'))
          .onClick(() => {
            this.imageName = Constants.TSF_BUTTON_IMAGE;
            testNapi.getImagePathAsyncTSF(this.imageName, (result: string) => {
              this.imagePath = Constants.IMAGE_ROOT_PATH + result;
            });
          })
      ...
      }
      ...
    }
    ...
  }
}
7.2.2 Native側(cè)開(kāi)發(fā)

導(dǎo)出Native接口:將Native接口導(dǎo)出到ArkTS側(cè),用于支撐ArkTS對(duì)象調(diào)用和模塊編譯構(gòu)建。

index.d.ts文件

// export thread safe function interface
export const getImagePathAsyncTSF: (imageName: string, callBack: (result: string) => void) => void;

全局變量定義:定義線程安全函數(shù)對(duì)象、隊(duì)列容量、初始化線程數(shù)。

MultiThreads.cpp文件

// define global thread safe function
static napi_threadsafe_function tsFun = nullptr;
static constexpr int MAX_MSG_QUEUE_SIZE = 0; // indicates that the queue length is not limited
static constexpr int INITIAL_THREAD_COUNT = 1;

call_js_cb回調(diào)處理定義:定義消費(fèi)者子線程中,通過(guò)napi_call_threadsafe_function拋到ArkTS主線程EventLoop中的回調(diào)處理函數(shù)。在該函數(shù)中,通過(guò)napi_call_function調(diào)用ArkTS應(yīng)用側(cè)傳入的回調(diào)callback,將異步任務(wù)搜索到的圖片路徑結(jié)果反饋到ArkTS應(yīng)用側(cè)。

MultiThreads.cpp文件

static void CallJsFunction(napi_env env, napi_value callBack, [[maybe_unused]] void *context, void *data) {
    // parse context data
    ContextData *contextData = static_cast<ContextData *>(data);
    // define the undefined variable, which is used in napi_call_function
    // because no other data is transferred, the variable is defined as undefined
    napi_value undefined = nullptr;
    napi_status operStatus = napi_get_undefined(env, &undefined);
    if (operStatus != napi_ok) {
        DeleteContext(env, contextData);
        return;
    }
    // convert the calculation result of C++ sub-thread to the napi_value type
    napi_value callBackArgs = nullptr;
    operStatus = napi_create_string_utf8(env, contextData->result.c_str(), contextData->result.length(), &callBackArgs);
    if (operStatus != napi_ok) {
        DeleteContext(env, contextData);
        return;
    }
    // call the JS callback and send the async calculation result on the Native to ArkTS application
    napi_value callBackResult = nullptr;
    (void)napi_call_function(env, undefined, callBack, 1, &callBackArgs, &callBackResult);
    // destroy data and release memory
    DeleteContext(env, contextData);
}

Native線程安全函數(shù)異步開(kāi)發(fā)接口:解析ArkTS應(yīng)用側(cè)參數(shù),使用napi_create_threadsafe_function創(chuàng)建線程安全函數(shù)對(duì)象,并在消費(fèi)者線程執(zhí)行函數(shù)ConsumeElementTSF中使用napi_call_threadsafe_function將異步回調(diào)處理函數(shù)call_js_cb拋到EventLoop中,等待調(diào)度執(zhí)行。

MultiThreads.cpp文件

// thread safe function async interface
static napi_value GetImagePathAsyncTSF(napi_env env, napi_callback_info info) {
    size_t paraNum = 2;
    napi_value paraArray[2] = {nullptr};
    // parse parameters
    napi_status operStatus = napi_get_cb_info(env, info, ?Num, paraArray, nullptr, nullptr);
    if (operStatus != napi_ok) {
        return nullptr;
    }
    napi_valuetype paraDataType = napi_undefined;
    operStatus = napi_typeof(env, paraArray[0], ?DataType);
    if ((operStatus != napi_ok) || (paraDataType != napi_string)) {
        return nullptr;
    }
    operStatus = napi_typeof(env, paraArray[1], ?DataType);
    if ((operStatus != napi_ok) || (paraDataType != napi_function)) {
        return nullptr;
    }
    // napi_value convert to char *
    constexpr size_t buffSize = 100;
    char strBuff[buffSize]{}; // char buffer for imageName string
    size_t strLength = 0;
    operStatus = napi_get_value_string_utf8(env, paraArray[0], strBuff, buffSize, &strLength);
    if ((operStatus != napi_ok) || (strLength == 0)) {
        return nullptr;
    }
    // async resource
    napi_value asyncName = nullptr;
    string asyncStr = "async napi_threadsafe_function";
    operStatus = napi_create_string_utf8(env, asyncStr.c_str(), asyncStr.length(), &asyncName);
    if (operStatus != napi_ok) {
        return nullptr;
    }
    // defines context data. the memory will be released in CompleteFunc
    auto contextData = new ContextData;
    contextData->args = strBuff;
    // create thread safe function
    if (tsFun == nullptr) {
        operStatus =
            napi_create_threadsafe_function(env, paraArray[1], nullptr, asyncName, MAX_MSG_QUEUE_SIZE,
                                            INITIAL_THREAD_COUNT, nullptr, nullptr, nullptr, CallJsFunction, &tsFun);
        if (operStatus != napi_ok) {
            DeleteContext(env, contextData);
            return nullptr;
        }
    }
    // create producer thread
    thread producer(ProductElement, static_cast<void *>(contextData));
    producer.detach(); // must be detached


    // create consumer thread
    thread consumer(ConsumeElementTSF, static_cast<void *>(contextData));
    consumer.detach();
    return nullptr;
}

八、完整代碼

ProducerConsumer.h

//
// Created on 2025/7/2.
//
// Node APIs are not fully supported. To solve the compilation error of the interface cannot be found,
// please include "napi/native_api.h".
// 生產(chǎn)者-消費(fèi)者模型實(shí)現(xiàn) 
// 模型緩沖隊(duì)列
#ifndef MultiThreads_ProducerConsumer_H
#define MultiThreads_ProducerConsumer_H


#include <string>
#include <queue>
#include <mutex>
#include <condition_variable>


using namespace std;
// Principles of the producer-consumer model: Use buffer zones to balance the rate between production and consumption.
// Synchronization relationship 1: When the buffer is full, the producer needs to be blocked and wait. When a product
// pops up the buffer, the producer needs to be woken up for consumption. Synchronization relationship 2: When the
// buffer is empty, the consumer needs to be blocked and waited. When a product enters the buffer, the consumer needs to
// be woken up for consumption.
class ProducerConsumerQueue {
public:
    // constructor
    ProducerConsumerQueue() {}
    ProducerConsumerQueue(int queueSize) : m_maxSize(queueSize) {}
    // producer enqueue operation
    void PutElement(string element);
    // consumer dequeue operation
    string TakeElement();
private:
    // check whether the buffer queue is full
    bool isFull() { return (m_queue.size() == m_maxSize); }
    // check whether the buffer queue is empty
    bool isEmpty() { return m_queue.empty(); }
private:
    queue<string> m_queue{};         // buffer queue
    int m_maxSize{};                 // buffer queue capacity
    mutex m_mutex{};                 // the mutex is used to protect data consistency
    condition_variable m_notEmpty{}; // condition variable, which is used to indicate whether the buffer queue is empty
    condition_variable m_notFull{};  // condition variable, which is used to indicate whether the buffer queue is full
};
#endif // MultiThreads_ProducerConsumer_H

ProducerConsumer.cpp

//
// Created on 2025/7/2.
//
// Node APIs are not fully supported. To solve the compilation error of the interface cannot be found,
// please include "napi/native_api.h".
// 生產(chǎn)者-消費(fèi)者模型實(shí)現(xiàn) 

#include "ProducerConsumer.h"


void ProducerConsumerQueue::PutElement(string element) {
    unique_lock<mutex> lock(m_mutex); // add mutex


    while (isFull()) {
        // when the data buffer queue is full, the production is blocked and wakes up after consumer consumption
        // m_mutex is automatically released at the same time
        m_notFull.wait(lock);
    }
    // reacquire the lock
    // if the queue is not full
    // the product is added to the queue and the consumer is notified that the product can be consumed
    m_queue.push(element);
    m_notEmpty.notify_one();
}
string ProducerConsumerQueue::TakeElement() {
    unique_lock<mutex> lock(m_mutex); // add mutex


    while (isEmpty()) {
        // when the data buffer queue is empty, the consumption is blocked and the producer is woken up after production
        // m_mutex is automatically released at the same time
        m_notEmpty.wait(lock);
    }
    // reacquire the lock
    // if the queue is not empty, the product is ejected and the producer is notified that it is ready for production
    string element = m_queue.front();
    m_queue.pop();
    m_notFull.notify_one();
    return element;
}

MultiThreads.cpp

/*
 * Copyright (c) 2024 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include "napi/native_api.h"
#include "ProducerConsumer.h"
#include <vector>
#include <thread>

using namespace std;

// define global thread safe function
static napi_threadsafe_function tsFun = nullptr;

static constexpr int MAX_MSG_QUEUE_SIZE = 0; // indicates that the queue length is not limited
static constexpr int INITIAL_THREAD_COUNT = 1;

// context data provided by users
// data is transferred between the native method (initialization data), ExecuteFunc, and CompleteFunc
struct ContextData {
    napi_async_work asyncWork = nullptr; // async work object
    napi_deferred deferred = nullptr;    // associated object of the delay object promise
    napi_ref callbackRef = nullptr;      // reference of callback
    string args = "";                    // parameters from ArkTS --- imageName
    string result = "";                  // C++ sub-thread calculation result --- imagePath
};

//定義一個(gè)靜態(tài)全局的模型緩沖隊(duì)列。由于,每次只處理一張圖片,所以將容量設(shè)定為1。
//
//定義一個(gè)靜態(tài)全局的圖片路徑集合,用于后文搜索。

// define the buffer queue and set the capacity to 1
static ProducerConsumerQueue buffQueue(1);

// defines the image path set, which is used for later search
static vector<string> imagePathVec{"sync.png", "callback.png", "promise.png", "tsf.png"};

// check the image paths based on the image name
static bool CheckImagePath(const string &imageName, const string &imagePath) {
    // separate character strings by suffix
    size_t pos = imagePath.find_first_of('.');
    if (pos == string::npos) {
        return false;
    }

    string nameTemp = imagePath.substr(0, pos);
    if (nameTemp.empty()) {
        return false;
    }

    // determine whether the image path is the target path based on whether the image names are the same
    return (imageName == nameTemp);
}

// search for image paths by image name
static string SearchImagePath(const string &imageName) {
    for (const string &imagePath : imagePathVec) {
        if (CheckImagePath(imageName, imagePath)) {
            return imagePath;
        }
    }

    return string("");
}
//生產(chǎn)者線程執(zhí)行函數(shù)
//生產(chǎn)者線程的執(zhí)行功能:通過(guò)解析上下文數(shù)據(jù)中的圖片名稱(chēng)參數(shù),來(lái)搜索相應(yīng)的圖片路徑,并將其置入模型緩沖隊(duì)列中。
static void ProductElement(void *data) {
    buffQueue.PutElement(SearchImagePath(static_cast<ContextData *>(data)->args));
}

//消費(fèi)者線程執(zhí)行函數(shù)
//消費(fèi)者線程的執(zhí)行功能:通過(guò)從模型緩沖隊(duì)列中獲取圖片路徑的搜索結(jié)果,并將其置入上下文數(shù)據(jù)中,用以后續(xù)反饋給ArkTS應(yīng)用側(cè)。
//非線程安全函數(shù)消費(fèi)者執(zhí)行函數(shù):
static void ConsumeElement(void *data) { static_cast<ContextData *>(data)->result = buffQueue.TakeElement(); }

//線程安全函數(shù)消費(fèi)者執(zhí)行函數(shù):
static void ConsumeElementTSF(void *data) {
    static_cast<ContextData *>(data)->result = buffQueue.TakeElement();
    // bind consumer thread to the thread safe function
    (void)napi_acquire_threadsafe_function(tsFun);
    // send async task to the JS main thread EventLoop
    (void)napi_call_threadsafe_function(tsFun, data, napi_tsfn_blocking);
    // release thread reference
    (void)napi_release_threadsafe_function(tsFun, napi_tsfn_release);
}

static void DeleteContext(napi_env env, ContextData *contextData) {
    // delete callback reference
    if (contextData->callbackRef != nullptr) {
        (void)napi_delete_reference(env, contextData->callbackRef);
    }

    // delete async work
    if (contextData->asyncWork != nullptr) {
        (void)napi_delete_async_work(env, contextData->asyncWork);
    }

    // release context data
    delete contextData;
}

static void ExecuteFunc([[maybe_unused]] napi_env env, void *data) {
    // create producer thread
    thread producer(ProductElement, data);
    // the producer and consumer threads must be synchronized
    // otherwise, the complete operation is triggered to communicate with the ArkTS after the executeFunc is complete
    // the result is unpredictable
    producer.join();

    // create consumer thread
    thread consumer(ConsumeElement, data);
    consumer.join();
}

static void CompleteFuncCallBack(napi_env env, [[maybe_unused]] napi_status status, void *data) {
    // parse context data
    ContextData *contextData = static_cast<ContextData *>(data);

    napi_value callBack = nullptr;
    napi_status operStatus = napi_get_reference_value(env, contextData->callbackRef, &callBack);
    if (operStatus != napi_ok) {
        DeleteContext(env, contextData);
        return;
    }

    // define the undefined variable, which is used in napi_call_function
    // because no other data is transferred, the variable is defined as undefined
    napi_value undefined = nullptr;
    operStatus = napi_get_undefined(env, &undefined);
    if (operStatus != napi_ok) {
        DeleteContext(env, contextData);
        return;
    }

    // convert the calculation result of C++ sub-thread to the napi_value type
    napi_value callBackArgs = nullptr;
    operStatus = napi_create_string_utf8(env, contextData->result.c_str(), contextData->result.length(), &callBackArgs);
    if (operStatus != napi_ok) {
        DeleteContext(env, contextData);
        return;
    }

    // call the JS callback and send the async calculation result on the Native to ArkTS application
    napi_value callBackResult = nullptr;
    (void)napi_call_function(env, undefined, callBack, 1, &callBackArgs, &callBackResult);

    // destroy data and release memory
    DeleteContext(env, contextData);
}

static void CompleteFuncPromise(napi_env env, [[maybe_unused]] napi_status status, void *data) {
    // parse context data
    ContextData *contextData = static_cast<ContextData *>(data);

    // convert the calculation result of C++ sub-thread to the napi_value type
    napi_value promiseArgs = nullptr;
    napi_status operStatus =
        napi_create_string_utf8(env, contextData->result.c_str(), contextData->result.length(), &promiseArgs);
    if (operStatus != napi_ok) {
        DeleteContext(env, contextData);
        return;
    }

    // the deferred and promise object are associated. the result is sent to ArkTS application through this interface
    operStatus = napi_resolve_deferred(env, contextData->deferred, promiseArgs);
    if (operStatus != napi_ok) {
        DeleteContext(env, contextData);
        return;
    }

    // destroy data and release memory
    DeleteContext(env, contextData);
}

static void CallJsFunction(napi_env env, napi_value callBack, [[maybe_unused]] void *context, void *data) {
    // parse context data
    ContextData *contextData = static_cast<ContextData *>(data);

    // define the undefined variable, which is used in napi_call_function
    // because no other data is transferred, the variable is defined as undefined
    napi_value undefined = nullptr;
    napi_status operStatus = napi_get_undefined(env, &undefined);
    if (operStatus != napi_ok) {
        DeleteContext(env, contextData);
        return;
    }

    // convert the calculation result of C++ sub-thread to the napi_value type
    napi_value callBackArgs = nullptr;
    operStatus = napi_create_string_utf8(env, contextData->result.c_str(), contextData->result.length(), &callBackArgs);
    if (operStatus != napi_ok) {
        DeleteContext(env, contextData);
        return;
    }

    // call the JS callback and send the async calculation result on the Native to ArkTS application
    napi_value callBackResult = nullptr;
    (void)napi_call_function(env, undefined, callBack, 1, &callBackArgs, &callBackResult);

    // destroy data and release memory
    DeleteContext(env, contextData);
}

// sync interface
static napi_value GetImagePathSync(napi_env env, napi_callback_info info) {
    size_t paraNum = 1;
    napi_value paraArray[1] = {nullptr};

    // parse parameters
    napi_status operStatus = napi_get_cb_info(env, info, ?Num, paraArray, nullptr, nullptr);
    if (operStatus != napi_ok) {
        return nullptr;
    }

    napi_valuetype paraDataType = napi_undefined;
    operStatus = napi_typeof(env, paraArray[0], ?DataType);
    if ((operStatus != napi_ok) || (paraDataType != napi_string)) {
        return nullptr;
    }

    // convert napi_value to char *
    constexpr size_t buffSize = 100;
    char strBuff[buffSize]{}; // char buffer for imageName string
    size_t strLength = 0;
    operStatus = napi_get_value_string_utf8(env, paraArray[0], strBuff, buffSize, &strLength);
    if ((operStatus != napi_ok) || (strLength == 0)) {
        return nullptr;
    }

    // defines context data. the memory will be released in CompleteFunc
    auto contextData = new ContextData;
    contextData->args = strBuff;

    // create producer thread
    thread producer(ProductElement, static_cast<void *>(contextData));
    producer.join();

    // create consumer thread
    thread consumer(ConsumeElement, static_cast<void *>(contextData));
    consumer.join();

    // convert the result to napi_value and send it to ArkTs application
    napi_value result = nullptr;
    (void)napi_create_string_utf8(env, contextData->result.c_str(), contextData->result.length(), &result);

    // delete context data
    DeleteContext(env, contextData);
    return result;
}

// callback async interface
static napi_value GetImagePathAsyncCallBack(napi_env env, napi_callback_info info) {
    size_t paraNum = 2;
    napi_value paraArray[2] = {nullptr};

    // parse parameters
    napi_status operStatus = napi_get_cb_info(env, info, ?Num, paraArray, nullptr, nullptr);
    if (operStatus != napi_ok) {
        return nullptr;
    }

    napi_valuetype paraDataType = napi_undefined;
    operStatus = napi_typeof(env, paraArray[0], ?DataType);
    if ((operStatus != napi_ok) || (paraDataType != napi_string)) {
        return nullptr;
    }

    operStatus = napi_typeof(env, paraArray[1], ?DataType);
    if ((operStatus != napi_ok) || (paraDataType != napi_function)) {
        return nullptr;
    }

    // napi_value convert to char *
    constexpr size_t buffSize = 100;
    char strBuff[buffSize]{}; // char buffer for imageName string
    size_t strLength = 0;
    operStatus = napi_get_value_string_utf8(env, paraArray[0], strBuff, buffSize, &strLength);
    if ((operStatus != napi_ok) || (strLength == 0)) {
        return nullptr;
    }

    // defines context data. the memory will be released in CompleteFunc
    auto contextData = new ContextData;
    contextData->args = strBuff;
    operStatus = napi_create_reference(env, paraArray[1], 1, &contextData->callbackRef);
    if (operStatus != napi_ok) {
        DeleteContext(env, contextData);
        return nullptr;
    }

    // async resource
    napi_value asyncName = nullptr;
    string asyncStr = "async callback";
    operStatus = napi_create_string_utf8(env, asyncStr.c_str(), asyncStr.length(), &asyncName);
    if (operStatus != napi_ok) {
        DeleteContext(env, contextData);
        return nullptr;
    }

    // create async work
    operStatus = napi_create_async_work(env, nullptr, asyncName, ExecuteFunc, CompleteFuncCallBack,
                                        static_cast<void *>(contextData), &contextData->asyncWork);
    if (operStatus != napi_ok) {
        DeleteContext(env, contextData);
        return nullptr;
    }

    // add the async work to the queue and wait for scheduling
    operStatus = napi_queue_async_work(env, contextData->asyncWork);
    if (operStatus != napi_ok) {
        DeleteContext(env, contextData);
    }

    return nullptr;
}

// promise async interface
static napi_value GetImagePathAsyncPromise(napi_env env, napi_callback_info info) {
    size_t paraNum = 1;
    napi_value paraArray[1] = {nullptr};

    // parse parameters
    napi_status operStatus = napi_get_cb_info(env, info, ?Num, paraArray, nullptr, nullptr);
    if (operStatus != napi_ok) {
        return nullptr;
    }

    napi_valuetype paraDataType = napi_undefined;
    operStatus = napi_typeof(env, paraArray[0], ?DataType);
    if ((operStatus != napi_ok) || (paraDataType != napi_string)) {
        return nullptr;
    }

    // napi_value convert to char *
    constexpr size_t buffSize = 100;
    char strBuff[buffSize]{}; // char buffer for imageName string
    size_t strLength = 0;
    operStatus = napi_get_value_string_utf8(env, paraArray[0], strBuff, buffSize, &strLength);
    if ((operStatus != napi_ok) || (strLength == 0)) {
        return nullptr;
    }

    // defines context data. the memory will be released in CompleteFunc
    auto contextData = new ContextData;
    contextData->args = strBuff;

    // async resource
    napi_value asyncName = nullptr;
    string asyncStr = "async promise";
    operStatus = napi_create_string_utf8(env, asyncStr.c_str(), asyncStr.length(), &asyncName);
    if (operStatus != napi_ok) {
        DeleteContext(env, contextData);
        return nullptr;
    }

    // create async work
    operStatus = napi_create_async_work(env, nullptr, asyncName, ExecuteFunc, CompleteFuncPromise,
                                        static_cast<void *>(contextData), &contextData->asyncWork);
    if (operStatus != napi_ok) {
        DeleteContext(env, contextData);
        return nullptr;
    }

    // add the async work to the queue and wait for scheduling
    operStatus = napi_queue_async_work(env, contextData->asyncWork);
    if (operStatus != napi_ok) {
        DeleteContext(env, contextData);
        return nullptr;
    }

    // create promise object
    napi_value promiseObj = nullptr;
    operStatus = napi_create_promise(env, &contextData->deferred, &promiseObj);
    if (operStatus != napi_ok) {
        DeleteContext(env, contextData);
        return nullptr;
    }

    return promiseObj;
}

// thread safe function async interface
static napi_value GetImagePathAsyncTSF(napi_env env, napi_callback_info info) {
    size_t paraNum = 2;
    napi_value paraArray[2] = {nullptr};

    // parse parameters
    napi_status operStatus = napi_get_cb_info(env, info, ?Num, paraArray, nullptr, nullptr);
    if (operStatus != napi_ok) {
        return nullptr;
    }

    napi_valuetype paraDataType = napi_undefined;
    operStatus = napi_typeof(env, paraArray[0], ?DataType);
    if ((operStatus != napi_ok) || (paraDataType != napi_string)) {
        return nullptr;
    }

    operStatus = napi_typeof(env, paraArray[1], ?DataType);
    if ((operStatus != napi_ok) || (paraDataType != napi_function)) {
        return nullptr;
    }

    // napi_value convert to char *
    constexpr size_t buffSize = 100;
    char strBuff[buffSize]{}; // char buffer for imageName string
    size_t strLength = 0;
    operStatus = napi_get_value_string_utf8(env, paraArray[0], strBuff, buffSize, &strLength);
    if ((operStatus != napi_ok) || (strLength == 0)) {
        return nullptr;
    }

    // async resource
    napi_value asyncName = nullptr;
    string asyncStr = "async napi_threadsafe_function";
    operStatus = napi_create_string_utf8(env, asyncStr.c_str(), asyncStr.length(), &asyncName);
    if (operStatus != napi_ok) {
        return nullptr;
    }

    // defines context data. the memory will be released in CompleteFunc
    auto contextData = new ContextData;
    contextData->args = strBuff;

    // create thread safe function
    if (tsFun == nullptr) {
        operStatus =
            napi_create_threadsafe_function(env, paraArray[1], nullptr, asyncName, MAX_MSG_QUEUE_SIZE,
                                            INITIAL_THREAD_COUNT, nullptr, nullptr, nullptr, CallJsFunction, &tsFun);
        if (operStatus != napi_ok) {
            DeleteContext(env, contextData);
            return nullptr;
        }
    }

    // create producer thread
    thread producer(ProductElement, static_cast<void *>(contextData));
    producer.detach(); // must be detached

    // create consumer thread
    thread consumer(ConsumeElementTSF, static_cast<void *>(contextData));
    consumer.detach();

    return nullptr;
}

EXTERN_C_START
static napi_value Init(napi_env env, napi_value exports) {
    napi_property_descriptor desc[] = {
        {"getImagePathSync", nullptr, GetImagePathSync, nullptr, nullptr, nullptr, napi_default, nullptr},
        {"getImagePathAsyncCallBack", nullptr, GetImagePathAsyncCallBack, nullptr, nullptr, nullptr, napi_default,
         nullptr},
        {"getImagePathAsyncPromise", nullptr, GetImagePathAsyncPromise, nullptr, nullptr, nullptr, napi_default,
         nullptr},
        {"getImagePathAsyncTSF", nullptr, GetImagePathAsyncTSF, nullptr, nullptr, nullptr, napi_default, nullptr}};
    napi_define_properties(env, exports, sizeof(desc) / sizeof(desc[0]), desc);
    return exports;
}
EXTERN_C_END

static napi_module demoModule = {
    .nm_version = 1,
    .nm_flags = 0,
    .nm_filename = nullptr,
    .nm_register_func = Init,
    .nm_modname = "entry",
    .nm_priv = ((void *)0),
    .reserved = {0},
};

extern "C" __attribute__((constructor)) void RegisterEntryModule(void) { napi_module_register(&demoModule); }

九、注意事項(xiàng)

線程安全:在異步開(kāi)發(fā)中,確保所有線程在執(zhí)行任務(wù)時(shí)不會(huì)發(fā)生數(shù)據(jù)競(jìng)爭(zhēng)或死鎖。

錯(cuò)誤處理:在處理圖片加載失敗的情況時(shí),及時(shí)拋出異常并反饋錯(cuò)誤信息。

性能優(yōu)化:通過(guò)優(yōu)化異步任務(wù)的執(zhí)行時(shí)間,減少主線程的阻塞等待時(shí)間。

資源管理:正確釋放隊(duì)列、線程和資源,避免資源泄漏和泄漏。

異常處理:確保所有異步任務(wù)都有明確的異常處理邏輯,避免程序崩潰或不響應(yīng)。 

十、個(gè)人評(píng)價(jià)

優(yōu)點(diǎn)

開(kāi)發(fā)流程清晰,代碼結(jié)構(gòu)合理,易于理解和維護(hù)。 靈活性高,支持多種場(chǎng)景的異步開(kāi)發(fā)。 線程安全機(jī)制完善,確保了任務(wù)執(zhí)行過(guò)程中的并發(fā)安全。 配套的接口文檔詳細(xì),便于開(kāi)發(fā)和調(diào)試。

缺點(diǎn)

需要有一定的C/C++開(kāi)發(fā)經(jīng)驗(yàn),才能熟練掌握異步開(kāi)發(fā)的技巧。 異步任務(wù)的執(zhí)行結(jié)果反饋機(jī)制需要額外的配置和測(cè)試。 在處理大規(guī)模數(shù)據(jù)或復(fù)雜場(chǎng)景時(shí),性能可能會(huì)有所瓶頸。

十一、更多案例

文件操作異步開(kāi)發(fā)

用戶(hù)可以選擇“文件操作異步調(diào)用”按鈕,啟動(dòng)異步文件讀取任務(wù)。 Native側(cè)將異步執(zhí)行文件讀取業(yè)務(wù)邏輯,不阻塞應(yīng)用側(cè)。 應(yīng)用側(cè)返回讀取結(jié)果,用于文件路徑刷新。

網(wǎng)絡(luò)請(qǐng)求異步開(kāi)發(fā)

用戶(hù)可以選擇“網(wǎng)絡(luò)請(qǐng)求異步調(diào)用”按鈕,啟動(dòng)異步網(wǎng)絡(luò)請(qǐng)求任務(wù)。 Native側(cè)將異步執(zhí)行網(wǎng)絡(luò)請(qǐng)求業(yè)務(wù)邏輯,不阻塞應(yīng)用側(cè)。 應(yīng)用側(cè)返回響應(yīng)結(jié)果,用于UI刷新。

數(shù)據(jù)庫(kù)操作異步開(kāi)發(fā)

用戶(hù)可以選擇“數(shù)據(jù)庫(kù)操作異步調(diào)用”按鈕,啟動(dòng)異步數(shù)據(jù)庫(kù)寫(xiě)入任務(wù)。 Native側(cè)將異步執(zhí)行數(shù)據(jù)庫(kù)寫(xiě)入業(yè)務(wù)邏輯,不阻塞應(yīng)用側(cè)。 應(yīng)用側(cè)返回寫(xiě)入結(jié)果,用于業(yè)務(wù)狀態(tài)刷新。

十二、總結(jié)

在線程安全開(kāi)發(fā)的基礎(chǔ)上,進(jìn)一步優(yōu)化線程調(diào)度和執(zhí)行效率,確保多線程場(chǎng)景下的性能穩(wěn)定。通過(guò)優(yōu)化異步工作項(xiàng)的執(zhí)行邏輯和隊(duì)列管理,提升異步任務(wù)的整體執(zhí)行效率。針對(duì)復(fù)雜的業(yè)務(wù)邏輯,設(shè)計(jì)更靈活的異步任務(wù)模型,確保異步開(kāi)發(fā)的靈活性和擴(kuò)展性。 使用性能監(jiān)控工具,分析異步任務(wù)的執(zhí)行時(shí)序和資源使用情況,優(yōu)化任務(wù)執(zhí)行效率。

通過(guò)以上步驟,用戶(hù)可以掌握基于Node-API的同步、異步和線程安全開(kāi)發(fā)技巧,實(shí)現(xiàn)高效、安全的圖片加載和相關(guān)業(yè)務(wù)邏輯。

相關(guān)文章

  • Android Adapter的幾個(gè)常用方法

    Android Adapter的幾個(gè)常用方法

    這篇文章主要為大家詳細(xì)介紹了Android Adapter的幾個(gè)常用方法,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2016-09-09
  • Android記事本項(xiàng)目開(kāi)發(fā)

    Android記事本項(xiàng)目開(kāi)發(fā)

    這篇文章主要為大家詳細(xì)介紹了Android記事本項(xiàng)目開(kāi)發(fā)的相關(guān)資料,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2017-12-12
  • Android使用Dialog風(fēng)格彈出框的Activity

    Android使用Dialog風(fēng)格彈出框的Activity

    這篇文章主要為大家詳細(xì)介紹了Android使用Dialog風(fēng)格彈出框的Activity,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2016-09-09
  • Kotlin定義其他類(lèi)的實(shí)現(xiàn)詳解

    Kotlin定義其他類(lèi)的實(shí)現(xiàn)詳解

    這篇文章主要介紹了Kotlin定義其他類(lèi)的實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2022-09-09
  • Android路由框架ARouter的使用示例

    Android路由框架ARouter的使用示例

    組件化或者模塊化開(kāi)發(fā)模式,已逐漸成為熱浪的形式,使用這些模式可以讓我們程序更容易的擴(kuò)展、更方便的維護(hù)、更快捷的同步開(kāi)發(fā)與更簡(jiǎn)單的單獨(dú)調(diào)試,而ARouter的出現(xiàn)就是讓組件間、模塊間是實(shí)現(xiàn)完全的獨(dú)立。ARouter主要解決組件間、模塊間的界面跳轉(zhuǎn)問(wèn)題。
    2021-06-06
  • android錯(cuò)誤 aapt.exe已停止工作的解決方法

    android錯(cuò)誤 aapt.exe已停止工作的解決方法

    這篇文章主要介紹了android錯(cuò)誤 aapt.exe已停止工作的解決方法,需要的朋友可以參考下
    2014-11-11
  • Android編程獲取sdcard音樂(lè)文件的方法

    Android編程獲取sdcard音樂(lè)文件的方法

    這篇文章主要介紹了Android編程獲取sdcard音樂(lè)文件的方法,涉及Android針對(duì)外部存儲(chǔ)卡中多媒體文件的相關(guān)操作技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下
    2015-11-11
  • Android SharedPreferences實(shí)現(xiàn)保存登錄數(shù)據(jù)功能

    Android SharedPreferences實(shí)現(xiàn)保存登錄數(shù)據(jù)功能

    這篇文章主要為大家詳細(xì)介紹了Android SharedPreferences實(shí)現(xiàn)保存登錄數(shù)據(jù)功能,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2019-05-05
  • Android實(shí)現(xiàn)QQ的第三方登錄和分享

    Android實(shí)現(xiàn)QQ的第三方登錄和分享

    這篇文章主要為大家詳細(xì)介紹了Android實(shí)現(xiàn)QQ的第三方登錄和分享,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2018-11-11
  • Android圖片的Base64編碼與解碼及解碼Base64圖片方法

    Android圖片的Base64編碼與解碼及解碼Base64圖片方法

    Base64是網(wǎng)絡(luò)上最常見(jiàn)的用于傳輸8Bit字節(jié)碼的編碼方式之一,Base64就是一種基于64個(gè)可打印字符來(lái)表示二進(jìn)制數(shù)據(jù)的方法。接下來(lái)通過(guò)本文給大家分享Android圖片的Base64編碼與解碼及解碼Base64圖片,需要的朋友參考下吧
    2017-12-12

最新評(píng)論

清原| 台北市| 揭阳市| 桃江县| 石河子市| 方正县| 昂仁县| 永定县| 醴陵市| 田东县| 琼结县| 西峡县| 石嘴山市| 翁牛特旗| 额济纳旗| 集贤县| 吉林省| 乐平市| 涞水县| 元江| 托里县| 宜章县| 教育| 万盛区| 北碚区| 扶余县| 布拖县| 遂平县| 牡丹江市| 会理县| 邢台县| 崇仁县| 富川| 渝中区| 正阳县| 伊宁市| 恩平市| 河曲县| 宁化县| 沭阳县| 清苑县|