Skip to content

[Proposal] - Thead Safe data structures #55568

Open
@HiImGiovi

Description

@HiImGiovi

What is the problem this feature will solve?

What I'd like to achieve is an implementation of shared (between worker threads) and thread safe data structures.
I want something easier to work with when doing multithread nodejs. Sharing data between threads using SharedArrayBuffer can be hard sometimes.

What is the feature you are proposing to solve the problem?

I'll show you an example of what the API usage could look like for a data structure like LRU Cache:

This is the code in the main thread

// main 
import {LRUCache} from "shared-ds"
import {Worker} from "worker_threads"

const cache = new LRUCache();

cache.set("test", { p1: "property1", p2: "property2" })

const worker = new Worker("./worker.js", {
    workerData: {
          lruCache: cache
    }
}

this is the worker code:

import { workerData } from "worker_threads";
import { LRUCache } from "shared-ds";


const cache = LruCache.fromWorkerData(workerData.lruCache);

const testValue = cache.get("test");
console.log(testValue); // this outputs { p1: "property1", p2: "property2" }

I started working into an implementation that uses extensively SharedArrayBuffer and Atomics to reach thread safety but there are lots of problems to take into consideration like:

  • Serialization of data into SharedArrayBuffer
  • atomics operations ( I still did not figure out how I can read multiple 'bytes atomically' avoiding multiple Atomic.load calls)

Do you guys think it's a useful feature and worth implementing or taking it into consideration?

What alternatives have you considered?

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    feature requestIssues that request new features to be added to Node.js.workerIssues and PRs related to Worker support.

    Type

    No type

    Projects

    Status

    Awaiting Triage

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions