Closed
Description
Beside wrapping napi_*_threadsafe
I'd also love to see a RunInNodeThread
implementation in node-addon-api
as an enabler for "concurrent processing" / multithreading and modern C++ with NAPI.
- see a usage example here
- and a possible implementation here
As napi_threadsafe..
API is not fire and forget, but creates objects etc., I wrapped everything in a class which must live on the heap, but threadsafe objects are created once and reused.
The main idea is that we can run RunInNodeThread
from any thread:
void js_fn(const Napi::CallbackInfo& info) {
// persist callback info[0]
std::thread([cb_ref]() { // or a thread pool or Rx
int result = heavyProcessing();
RunInNodeThread([result, cb_ref](Napi::Env env) {
// callback cb_ref with result in node thread
});
}
}
What do you think?