Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 556
Jpark9061 Avatar asked Jul 13 '20 22:07

Jpark9061


People also ask

How do worker threads work node JS?

Instead, the worker threads approach allows applications to use several isolated JavaScript workers, with Node providing communication between workers and the parent worker. Each worker in Node. js has its own V8 and Event Loop instance. Workers, unlike children's processes, can exchange memory.

What is worker thread in multithreading?

A worker thread is commonly used to handle background tasks that the user should not have to wait for to continue using your application. Tasks such as recalculation and background printing are good examples of worker threads.

What are worker threads?

Worker thread is a continuous parallel thread that runs and accepts messages until the time it is explicitly closed or terminated. Messages to a worker thread can be sent from the parent thread or its child worker threads. Through out this document, parent thread is referred as thread where a worker thread is spawned.

What is cluster and worker thread in node JS?

Threads share memory (e.g. SharedArrayBuffer ) whereas processes don't. Essentially they are the same thing categorically. cluster. One process is launched on each CPU and can communicate via IPC. Each process has it's own memory with it's own Node (v8) instance.

What is workerthreads in Node JS?

In Node.js, a process is able to have multiple threads of JavaScript now (using WorkerThreads). These run independently so you can get true parallelization of running JavaScript in multiple threads concurrently.

What is multithreading in Node JS?

So, What Is Multithreading in Node.js? Multithreading is a program execution model that allows multiple threads to be created within a process. The threads execute independently but concurrently share process resources. To understand multithreading, we need to know how a single-thread process looks. Imagine we have a set of four instructions.

How many threads does NodeJS use?

Node.js uses two kinds of threads: a main thread handled by event loop and several auxiliary threads in the worker pool. Additionally, this article I read said the statement above. This makes it sound like JavaScript was actually using multiple different threads the entire time.

Is Javascript single-threaded?

Or is JavaScript indeed permanently single threaded, but with the power of worker threads, a process is able to have multiple threads of JavaScript, which still behave single thread? Node.js uses two kinds of threads: a main thread handled by event loop and several auxiliary threads in the worker pool.


1 Answers

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.

like image 79
jfriend00 Avatar answered Nov 04 '22 03:11

jfriend00