carduh carduh - 4 years ago 69
C++ Question

Nodejs C/C++ Will use more than one Core?

I know for a fact that NodeJS runs in a single core. But, what happens if I build a NodeJS C/C++ Addon which has multithreading implemented? This addon will consume more than one core (when it is supposed)?

Answer Source

node.js uses only one thread/core for running the Javascript interpreter.

But node.js does and can use more than one thread/core in various library functions or add-on functions such as file I/O that are implemented in C++. In fact, the libuv library that node.js uses for file I/O uses a thread poll as part of its implementation of asynchronous, non-blocking file I/O. This doesn't affect the "single threadedness" of the Javascript interpreter, but does aid in the implementation of external functions such as asynchronous file I/O.

So, if your add-on uses more than one thread in its C++, then that is all fine. It will have to communicate back to the one main thread that is running the Javascript interpreter.

But, what happens if I build a NodeJS C/C++ Addon which has multithreading implemented?

Your C++ addon is free to use multiple threads in its implementation if that is desired. If it has multiple threads running, it will have to communicate with the main Javascript thread in a thread-safe manner.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download