Calling napi_call_function from a worker thread #39658
Unanswered
tinpotnick
asked this question in
General
Replies: 1 comment
-
After some reading time. I think I have the answer. To prevent node from exiting whilst the addon is busy: Point 1The section under Asynchronous thread-safe function calls in the main node addon page explains threads. i.e. only the node thread can call back into and goes on to explain it in detail. Not sure how I missed that. Point 2To stop node exiting when there is an async function being performed is to create some async work: static void runworkcomplete( napi_env env, napi_status status, void *data ) {
napi_value resolution;
napi_create_string_utf8( env, "Server stopped", NAPI_AUTO_LENGTH, &resolution );
napi_resolve_deferred( env, stoppingdefferedpromise, resolution );
}
static napi_value stopserver( napi_env env, napi_callback_info info ) {
napi_value stoppingpromise;
if( napi_ok != napi_create_promise( env, &stoppingdefferedpromise, &stoppingpromise ) ) {
return NULL;
}
// Stop our server
return stoppingpromise;
}
static napi_value startserver( napi_env env, napi_callback_info info ) {
napi_value workname;
napi_async_work workhandle;
if( napi_ok != napi_create_string_utf8( env, "projectrtp", NAPI_AUTO_LENGTH, &workname ) ) {
return NULL;
}
if( napi_ok != napi_create_async_work( env,
NULL,
workname,
runwork,
runworkcomplete,
NULL, /*void* data,*/
&workhandle ) ) {
return NULL;
}
if( napi_ok != napi_queue_async_work( env, workhandle ) ) {
return NULL;
}
return NULL;
} |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
Hi,
I am trying to write a C++ addon. In the addon, it will create worker threads that will process network audio. When an event occurs in the worker thread I need to pass this back into the node application.
Then in C++ something like
I have seen in the documentation napi_call_threadsafe_function. I anticipate this works something like:
Beta Was this translation helpful? Give feedback.
All reactions