Native如何创建子线程,有什么约束,与主线程如何通信

​Native如何创建子线程,有什么约束,与主线程如何通信

HarmonyOS
2024-03-17 13:54:28
浏览
收藏 0
回答 1
待解决
回答 1
按赞同
/
按时间
wjmfzsz

请参照下面的代码,通过C++子线程调用arkts侧的函数:

#include "napi/native_api.h" 
#include "hilog/log.h" 
#include "thread" 
 
napi_ref cbObj = nullptr; 
napi_threadsafe_function tsfn; 
#define NUMBER 666 
static void CallJs(napi_env env, napi_value js_cb, void *context, void *data) { 
    napi_get_reference_value(env, cbObj, &js_cb); 
    napi_value argv; 
    napi_create_int32(env, NUMBER, &argv); 
    napi_value result = nullptr; 
    napi_call_function(env, nullptr, js_cb, 1, &argv, &result); 
} 
static napi_value ThreadsTest(napi_env env, napi_callback_info info) { 
    size_t argc = 1; 
    napi_value js_cb, work_name; 
    napi_status status; 
    status = napi_get_cb_info(env, info, &argc, &js_cb, nullptr, nullptr); 
    OH_LOG_INFO(LOG_APP, "ThreadSafeTest 0: %{public}d", status == napi_ok); 
    // Set initial_refcount to 0 for a weak reference, >0 for a strong reference. 
    status = napi_create_reference(env, js_cb, 1, &cbObj); 
    OH_LOG_INFO(LOG_APP, "napi_create_reference of js_cb to cbObj: %{public}d", status == napi_ok); 
    status = napi_create_string_utf8(env, "Work Item", NAPI_AUTO_LENGTH, &work_name); 
    status = napi_create_threadsafe_function(env, js_cb, NULL, work_name, 0, 1, NULL, NULL, NULL, CallJs, &tsfn); 
    OH_LOG_INFO(LOG_APP, "napi_create_threadsafe_function : %{public}d", status == napi_ok); 
    std::thread t({ 
        std::thread::id this_id = std::this_thread::get_id(); 
        OH_LOG_INFO(LOG_APP, "thread0 %{public}d.\n", this_id); 
        napi_status status; 
        status = napi_acquire_threadsafe_function(tsfn); 
        OH_LOG_INFO(LOG_APP, "thread : %{public}d", status == napi_ok); 
        napi_call_threadsafe_function(tsfn, NULL, napi_tsfn_blocking); 
    }); 
    t.detach(); 
    return NULL; 
}
分享
微博
QQ
微信
回复
2024-03-18 16:49:16
相关问题
TaskPool线程主线程如何通信
2327浏览 • 1回复 待解决
TaskPool如何主线程进行通信
562浏览 • 1回复 待解决
IPCRPC通信什么约束限制?
338浏览 • 1回复 待解决
如何判断当前线程是否是主线程
2436浏览 • 1回复 待解决
Native创建线程是否有限制
2180浏览 • 1回复 待解决
Worker的宿主线程必须是主线程吗?
414浏览 • 1回复 待解决