Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add an example 'shared-resource' #23

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ await locks.request('Resource name', async lock => {
});
```

More examples [here](./examples/README.md)

## License

This implementation of Web Locks API is [MIT licensed](./LICENSE).
7 changes: 7 additions & 0 deletions examples/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
## shared-resource

* A simple example of parallel working on the same resource from several threads
* run
```bash
node examples/shared-resource/thread-main.js
```
44 changes: 44 additions & 0 deletions examples/shared-resource/thread-main.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
'use strict';

const path = require('path');
const threads = require('worker_threads');
const { locks } = require('../..');

const startWork = () =>
new Promise(resolve => {
const workerFile = path.resolve(__dirname, 'thread-worker.js');

const resource = new SharedArrayBuffer(8);

const worker1 = new threads.Worker(workerFile, { workerData: resource });
const worker2 = new threads.Worker(workerFile, { workerData: resource });
const worker3 = new threads.Worker(workerFile, { workerData: resource });

locks.attach(worker1);
locks.attach(worker2);
locks.attach(worker3);

let counter = 0;
const done = message => {
if (message.status === 'done') {
counter++;
if (counter === 3) {
console.log('After processing:', resource);

resolve();
}
}
};

worker1.on('message', done);
worker2.on('message', done);
worker3.on('message', done);

locks.request('resource', async () => {
worker1.postMessage({ isMyProgram: true, task: 'someWorkWithData' });
worker2.postMessage({ isMyProgram: true, task: 'someWorkWithData' });
worker3.postMessage({ isMyProgram: true, task: 'someWorkWithData' });
});
});

startWork();
38 changes: 38 additions & 0 deletions examples/shared-resource/thread-worker.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
'use strict';

const threads = require('worker_threads');
const { locks } = require('../../');

const SLEEPING_TIME_MS = 1000;

const sleep = () =>
new Promise(resolve => {
setTimeout(resolve, SLEEPING_TIME_MS);
});

const someWorkWithData = async () => {
await locks.request('resource', {}, async () => {
await sleep();
const view = new Int8Array(threads.workerData);
for (let i = 0; i < view.length; i++) {
view[i] *= threads.threadId;
view[i] += threads.threadId;
}

console.log('info', threads.threadId, view);
});
threads.parentPort.postMessage({ status: 'done' });
process.exit(0);
};

const worksMapper = {
someWorkWithData,
};

threads.parentPort.on('message', async message => {
if (!message.isMyProgram) {
return;
}

await worksMapper[message.task]();
});