2

So this is my understanding of how node works:

  • Single thread in which all your JS code executes.
  • IO/Network call utilizes threadpools behind the scene. (using libuv which is a C++ lib)
  • Once an IO operation is done, it's callback is pushed on the callback queue, and the callback can be picked up by event loop in next tick.

What I want to understand is how do I write code that can utilize system threads? Do I write a lib in C/C++ and provide a Javascript bindings for that?

Let's say I have some operation/function which doesn't need IO but is CPU intensive, so I'd like to run it in multiple threads. How would I do that in Node?

Jatin
  • 14,112
  • 16
  • 49
  • 78

3 Answers3

0

My understanding is that this can't be done with Node. You could, of course, write a program in another language and program some bindings, but Node itself is not multi threading capable. You can use multi-programming instead: Start multiple instances of your Node program and exchange messages via HTTP. Your operating system would then handle the distribution of your program instances on different CPU cores.

Daniel Diekmeier
  • 3,401
  • 18
  • 33
0

Well Node uses libuv, which implements a thread pool. Have a read here for a nice explanation. To cherry pick an insight from the article

The libuv library maintains a pool of threads that is used by node.js to perform long-running operations in the background, without blocking its main thread. Effectively, deep under the hood, node.js is thread-based, whether you like it or not.

So perhaps investigate how libuv does this, and how it interacts with Node, and base your own approach on that?

This answer explains things nicely, showing some of the libuv code, and references another great article on libuv.

Community
  • 1
  • 1
Philip O'Brien
  • 4,146
  • 10
  • 46
  • 96
0

Node being single threaded is a common misconception. The truth is much more complex. While the Node Event Loop is single threaded, some of the functions included in the Node standard library are not single threaded.

Some of the functions included inside of Node that we run are running outside the Event Loop and therefore outside that single thread.

So a lot of the code we write in Node does not execute inside that Single Thread entirely.

You can test this out for yourself by writing code that gives you real proof that not all of Node is truly single-threaded by implementing the pbkdf2 function from the crypto module.

You can benchmark how long the pbkdf2 function takes to run to get a sense of how threading and threadpools actually are working on your machine. So you can create various pbkdf2 functions to represent the threads. The function will do some expensive work and you can benchmark that and use it to detect whether Node is single threaded like so:

const crypto = require('crypto');

const start = Date.now();
crypto.pbkdf2('a', 'b', 100000, 512, 'sha512', () => {
  console.log('1:', Date.now() - start);
});

This function takes quite awhile to run, so about 1 second in most mid-2015 MacBook Pros.

So the callback function above is computing a hash which again with the machine I indicated above should take no more than 1 second to complete, but what the function does is not as important as how long it takes to execute on your machine.

You can add more function calls to represent more threads and you can also tweak the threadpool size in your machines threadpool like so process.env.UV_THREADPOOL_SIZE = 2;.

Daniel
  • 14,004
  • 16
  • 96
  • 156