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

1

6

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

Rosyrot answered 13/7, 2020 at 22:14 Comment(2)
This is a pretty good article with a simple example. The idea is, use worker threads for CPU-intensive processes, like calculating a list of primes (the example in the article). This article has a better explanation of how node.js stays single-threaded yet uses worker threads.Camargo
On Linux there is very little difference between threads and processes since they are implemented with the same primitives but with different configurations. On Windows and MacOS there's a big difference between processes and threads.Palmieri
K
10

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.

Khmer answered 13/7, 2020 at 23:53 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.