Axel
Axel

Reputation: 483

C++ add-ons to Node, non blocking?

Making a fairly large web application with Node, and running into the concept of c++ add-ons, and being completely new to the concept, I wonder; are add-ons written in C++ for Node.js blocking the thread?

For example a mathematical algorithm, running asynchronous of course, that if implemented in JS would block the thread.

Upvotes: 4

Views: 220

Answers (1)

jfriend00
jfriend00

Reputation: 707158

Native code add-ons can be written either blocking or non-blocking. For example, fs.readFile() and fs.readFileSync() each have native code add-on implementations (they happen to be built-in add-ons, but the interface is largely the same). So, it depends upon how you implement the add-on functions for whether they will have an asynchronous interface (and communicate results back via the event queue) or just be blocking functions.

A mathematical computation in native add-on code would have to create it's own native thread or process in order to run asynchronously and non-blocking. But this can certainly be done.

You could prototype something like this by just putting the code in another process (written in any language) and then communicating with it from node.js via an http interface. You don't even have to do it via a node.js add-on.

Upvotes: 3

Related Questions