Worker Threads and Mutual Exclusion in Node.js

CK CK 3788 
Created at  
462 0 0

In Node.js, true multi-threading is not natively supported due to its single-threaded, event-driven architecture. However, you can achieve concurrency using worker threads and manage mutual exclusion using mechanisms like locks or atomic operations.

Here’s how you can create a simple example of threads and mutual exclusion in Node.js using the worker_threads module and a basic lock mechanism:

 

Step 1: Install Necessary Modules

Ensure you have Node.js installed, as worker_threads is a built-in module starting from Node.js 10.5.0.

 

Step 2: Create a Shared Resource with Mutual Exclusion

File: counter.js

// counter.js

let count = 0;
let lock = false;

function increment() {
  while (lock) {
    // Busy-wait (not ideal in real-world scenarios, but illustrates the concept)
  }
  lock = true;
  count++;
  lock = false;
  return count;
}

module.exports = increment;

 

Step 3: Create a Worker Thread to Modify the Shared Resource

File: worker.js

// worker.js

const { parentPort } = require('worker_threads');
const increment = require('./counter');

parentPort.on('message', () => {
  const newCount = increment();
  parentPort.postMessage(newCount);
});

 

Step 4: Create the Main Thread to Spawn Worker Threads

File: main.js

// main.js

const { Worker } = require('worker_threads');

const numWorkers = 5;

for (let i = 0; i < numWorkers; i++) {
  const worker = new Worker('./worker.js');

  worker.on('message', (count) => {
    console.log(`Worker ${i + 1}: Count is now ${count}`);
  });

  worker.postMessage('increment'); // Trigger the worker to increment the count
}

 

Explanation

  1. Shared Resource (counter.js):
    • This file defines a shared counter variable and a simple lock mechanism to prevent race conditions.
    • The increment() function increases the counter and ensures mutual exclusion using the lock variable.
  2. Worker Thread (worker.js):
    • The worker listens for messages from the parent thread (main.js) and increments the counter by calling the increment() function from counter.js.
    • The result is sent back to the parent thread.
  3. Main Thread (main.js):
    • This file creates multiple worker threads.
    • Each worker increments the shared counter and sends the updated value back to the main thread.
    • The main thread logs the updated count to the console.

 

Step 5: Run the Application

To run this example:

node main.js

 

Output

You should see output similar to:

Worker 1: Count is now 1
Worker 2: Count is now 2
Worker 3: Count is now 3
Worker 4: Count is now 4
Worker 5: Count is now 5

 

Notes

  1. Busy-Waiting: The while (lock) {} loop in counter.js is a simple but inefficient method to implement mutual exclusion. In a real-world application, you would want to use more sophisticated techniques like Atomics or third-party libraries for locking mechanisms.
  2. Concurrency, Not Parallelism: Node.js uses a single event loop for non-blocking I/O operations. The worker_threads module provides a way to achieve parallelism by running JavaScript in separate threads, making it useful for CPU-bound tasks.
  3. Avoid Race Conditions: The lock mechanism prevents race conditions in this example. In more complex applications, you'd use more advanced synchronization techniques.

This example demonstrates basic mutual exclusion in a multi-threaded Node.js environment using worker_threads.

Tags Node.js Thread Thread Programming counter.js main.js worker.js worker_threads Facebook X
Comments 0
Similar posts
  1. Trackback module used for Python Threading debugging
    843
  2. Thread Implementation in Python
    509
  1. Get client IP address and user agent in Node.js
    1,530
  2. forEach method in JavaScript when working with arrays
    665
  3. CRC32B function code in Javascript
    1,839
  4. Node.js-based Web Server supporting multiple path
    598
  5. A simple Javascript class example with static method for Node.js
    660
  6. Setting a Common Path for require() in Node.js
    993
  7. Import all JS files in a specific folder for Node.js programming
    1,724
  8. A simple example to access a PostgreSQL database in Node.js
    1,100
  9. Node.js code example to access Google BigQuery
    1,181
  10. Investigation on package.js for Node.js source code
    819
  11. Using MySQL in Node.js
    481
  12. A Node.js script that checks if a module is installed before requiring it
    770
  13. Using a module in the external class itself without creating an instance in Node.js
    349
  14. Node.js external modules
    430
  15. Multi-threaded Web Server based in Node.js
    864
  16. A basic HTTP server based in Node.js
    538
Recently updated
  1. Why 100% SEO Guarantees Better User Engagement and How to Achieve It in Your Service
    42
  2. Why SPF and DKIM Are Essential for Linux Email Servers?
    36
  3. How do I create animated background for Google Meet?
    21,044
  4. MaxMind GeoLite2 vs. DB-IP Lite vs. ip-api.com: Choosing the Right Free IP Geolocation Solution
    252
  5. Meet Valkey: Why Redis Changed Its License, Why Valkey is Better, and How to Use It as a Drop-in Replacement
    94
  6. How Rogue Web Scrapers Are Stealing My Content and Destroying My Server
    157
  7. Array of arguments passed to script in PHP
    90
  8. Invalid Auction Storms in Ad Tech
    98
  9. Building MRC-Compliant Instrumentation in Ad Tech: Implementation Steps and Key Challenges
    202
  10. The Two Clocks of Ad Server Architecture
    70
  11. Frequency Cap Architecture Overview
    515
  12. Collecting Proof-of-Play from 100,000 Screens
    146
  13. Vector Search in Relational Databases (RDBs): Trends & Usage Environments
    288
  14. What is RedisVL?
    109
  15. Designing a Skill Registry Architecture: Minimize Token Usage and Maximize AI Performance
    232
  16. TPU & GPU Cluster Management Tools
    245
  17. The Concept of Scaffolding in AI Agents
    234
  18. Beyond Static Pipelines: Architecting LLM Multi-Agent Systems for Autonomous Workflow Orchestration
    583
  19. AI-DLC vs. AI-SDLC: Model-Centric vs. System-Centric AI Development
    259
  20. Fine-Tuning Open-Weight LLMs: Training, Licensing, and Deploying Custom Models with Ollama
    339
  21. Inside the Next-Generation Streaming Architecture: Ultra-Low Latency, SGAI, and AI Codecs
    202
  22. Machine Learning: Training vs. Ranking
    222
  23. The Continuous Learning Machine: Designing a Loop That Actually Turns
    222
  24. The Next-Gen Low-Latency Standards: LL-HLS & MoQ
    195
  25. Designing a Data Curation System Around a Vector Database
    221
  26. Extracting Context from Live Video: The Architecture Got Simpler When We Stopped Building Features
    256
  27. The New Ad Stack Behind Live Sports Streaming: Inside SGAI, EABN, and the Standards Race to Fix Live Monetization
    656
  28. Designing MLOps Architectures to Handle Seasonal Data Drift in EV Fleet Models
    196
  29. From 15 to 40+ FPS: Accelerating PyTorch Object Detection on Automotive ECUs
    219
  30. Understanding Data Lineage
    263