napi_create_threadsafe_function


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); 
  • [in] env:调用 API 的环境。

    ¥[in] env: The environment that the API is invoked under.

  • [in] func:从另一个线程调用的可选 JavaScript 函数。如果 NULL 传递给 call_js_cb,则必须提供它。

    ¥[in] func: An optional JavaScript function to call from another thread. It must be provided if NULL is passed to call_js_cb.

  • [in] async_resource:与将传递给可能的 async_hooks init 钩子 的异步工作关联的可选对象。

    ¥[in] async_resource: An optional object associated with the async work that will be passed to possible async_hooks init hooks.

  • [in] async_resource_name:一个 JavaScript 字符串,用于为 async_hooks API 公开的诊断信息提供的资源类型提供标识符。

    ¥[in] async_resource_name: A JavaScript string to provide an identifier for the kind of resource that is being provided for diagnostic information exposed by the async_hooks API.

  • [in] max_queue_size:队列的最大大小。0 为无限制。

    ¥[in] max_queue_size: Maximum size of the queue. 0 for no limit.

  • [in] initial_thread_count:初始获取数,即初始线程数,包括将使用此函数的主线程。

    ¥[in] initial_thread_count: The initial number of acquisitions, i.e. the initial number of threads, including the main thread, which will be making use of this function.

  • [in] thread_finalize_data:要传递给 thread_finalize_cb 的可选数据。

    ¥[in] thread_finalize_data: Optional data to be passed to thread_finalize_cb.

  • [in] thread_finalize_cbnapi_threadsafe_function 被销毁时调用的可选函数。

    ¥[in] thread_finalize_cb: Optional function to call when the napi_threadsafe_function is being destroyed.

  • [in] context:附加到生成的 napi_threadsafe_function 的可选数据。

    ¥[in] context: Optional data to attach to the resulting napi_threadsafe_function.

  • [in] call_js_cb:可选回调调用 JavaScript 函数以响应不同线程上的调用。此回调将在主线程上调用。如果没有给出,JavaScript 函数将在没有参数的情况下调用,并将 undefined 作为其 this 值。napi_threadsafe_function_call_js 提供了更多详细信息。

    ¥[in] call_js_cb: Optional callback which calls the JavaScript function in response to a call on a different thread. This callback will be called on the main thread. If not given, the JavaScript function will be called with no parameters and with undefined as its this value. napi_threadsafe_function_call_js provides more details.

  • [out] result:异步线程安全的 JavaScript 函数。

    ¥[out] result: The asynchronous thread-safe JavaScript function.

变更历史:

¥Change History:

  • 实验性的(定义了 NAPI_EXPERIMENTAL):

    ¥Experimental (NAPI_EXPERIMENTAL is defined):

    call_js_cb 中引发的未捕获异常将由 'uncaughtException' 事件处理,而不是被忽略。

    ¥Uncaught exceptions thrown in call_js_cb are handled with the 'uncaughtException' event, instead of being ignored.