Search code examples
javascriptnode.jsmultithreadingsingle-threadedworker-thread

Node.js multithreading: What are Worker threads and how does it work?


I always believed that JS was a single threaded language which makes it inefficient for CPU intensive tasks. I recently came across worker threads and how it solves this inefficiency problem by creating "multiple worker threads under one process". What's the difference between a process and a thread? Why is JS all of the sudden capable of spawning multiple worker threads that help and interact with the main JS thread to enable concurrency? Could you help me understand this topic in layman terms? Thank you


Solution

  • Starting in node v10, they introduced WorkerThreads. A WorkerThread is an entirely new instance of the V8 Javascript interpreter. It has it's own set of variables, it's own globals and it's own thread of running Javascript. You cannot directly share regular Javascript variables between the main thread and a workerThread or between workerThreads.

    You can directly share memory if it is specifically allocated as SharedMemory such as a SharedArrayBuffer, but when doing so, you open yourself up to race conditions between the two threads both accessing the Shared memory. So, you have to either use Atomics or your own concurrency management scheme to prevent race conditions when modifying the shared memory.

    The main thread and workerThreads can send messages to each other and those messages can contain some types of data structures that will be copied via a structured cloning mechanism and sent to the other V8 instance.

    The idea behind workerThreads is that they are useful for getting CPU-intensive code out of your main event loop (particularly useful for servers) so you can fire up one or more workerThreads to handle CPU-intensive work and keep the main thead event loop free and responsive to incoming events/networking/etc...

    You can also do something similar by creating multiple nodejs processes. But, a process is a heavier-weight thing than a workerThread and workerThreads allow you to share memory with SharedMemory whereas separate processes do not.