Skip to content

threadsafe: RunInNodeThread #413

Closed
Closed
@DaAitch

Description

@DaAitch

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.

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?

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions