{"id":17592542,"url":"https://github.com/kromdaniel/web-mutex","last_synced_at":"2025-04-29T17:21:14.235Z","repository":{"id":143868522,"uuid":"162687120","full_name":"KromDaniel/web-mutex","owner":"KromDaniel","description":"Node.js / Browser Mutex implementation for Worker threads :lock:","archived":false,"fork":false,"pushed_at":"2019-01-22T11:29:41.000Z","size":85,"stargazers_count":7,"open_issues_count":0,"forks_count":0,"subscribers_count":3,"default_branch":"master","last_synced_at":"2025-04-20T09:18:22.575Z","etag":null,"topics":["atomic","multithreading","mutex","synchronization","web-worker","worker-threads"],"latest_commit_sha":null,"homepage":"","language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/KromDaniel.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2018-12-21T08:21:59.000Z","updated_at":"2024-11-21T10:33:18.000Z","dependencies_parsed_at":null,"dependency_job_id":"ef1ef845-e8da-4e3a-8791-40d2dc37e271","html_url":"https://github.com/KromDaniel/web-mutex","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/KromDaniel%2Fweb-mutex","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/KromDaniel%2Fweb-mutex/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/KromDaniel%2Fweb-mutex/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/KromDaniel%2Fweb-mutex/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/KromDaniel","download_url":"https://codeload.github.com/KromDaniel/web-mutex/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":251546717,"owners_count":21606892,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["atomic","multithreading","mutex","synchronization","web-worker","worker-threads"],"created_at":"2024-10-22T05:23:45.725Z","updated_at":"2025-04-29T17:21:14.210Z","avatar_url":"https://github.com/KromDaniel.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Still under construction\n\nThis package helps with threads synchronization.\u003cbr/\u003e\n\nNode.js - [Worker threads](https://nodejs.org/api/worker_threads.html)\u003cbr/\u003e\nBrowser - [Web Worker](https://developer.mozilla.org/en-US/docs/Web/API/Web_Workers_API/Using_web_workers)\n\nThe package uses [Atomics](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Atomics) API behind the scenes and implements different locks.\n\n## Installation\n\n```\nnpm i web-mutex\n```\n\n## Web Mutex\n---\n### Motivation\nUsing threads we can achieve better concurrency and parallelism\n\n#### Single thread\n28709.764ms\n```js\nconst ARR_SIZE = 2 ** 25;\nconst singleThreadedArr = Array(ARR_SIZE);\n\n console.time('SINGLE THREAD');\nfor (let i = 0; i \u003c ARR_SIZE; i++) {\n    const num = i + 2;\n    singleThreadedArr[i] = isPrime(num) ? num : -1\n}\nconsole.timeEnd('SINGLE THREAD'); //  28709.764ms\n```\n\n#### Multi thread\n1978.282ms\n```js\nimport { Worker, isMainThread, parentPort, threadId } from 'worker_threads';\nimport { SyncedValue } from 'web-mutex';\n\nif (isMainThread) {\n    // shared array buffer for locks\n    const synced = new SharedArrayBuffer(Int32Array.BYTES_PER_ELEMENT);\n    const numbers = new SharedArrayBuffer(Int32Array.BYTES_PER_ELEMENT * ARR_SIZE);\n    // create the threads\n    const workers = Array(4).fill(0).map(() =\u003e new Worker(__filename));\n    console.time('MULTI THREAD');\n    workers.forEach((w) =\u003e w.postMessage({ numbers, synced }));\n     /**\n     * The main thread will wait for all worker threads to finish\n     */\n    process.on('beforeExit', () =\u003e {\n        console.timeEnd('MULTI THREAD'); // 1978.282ms\n    });\n   \n} else {\n    // this scope represents run of a worker thread\n    parentPort.on('message', ({ numbers, synced }) =\u003e {\n        const syncedValue = new SyncedValue(new Int32Array(synced), 0);\n        const arr = new Int32Array(numbers);\n        for (let i = syncedValue.incrementOne(); i \u003c arr.length; i = syncedValue.incrementOne()) {\n            const num = arr[i] + 2;\n            arr.set([isPrime(num) ? num : -1], i);\n        }\n        parentPort.close();\n    });\n}\n```\n\n\n\n\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fkromdaniel%2Fweb-mutex","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fkromdaniel%2Fweb-mutex","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fkromdaniel%2Fweb-mutex/lists"}