Skip to content

Instantly share code, notes, and snippets.

@mafintosh
Last active August 21, 2022 21:12
Show Gist options
  • Star 5 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mafintosh/8f5d3076a1afd599cbd9bd5289595179 to your computer and use it in GitHub Desktop.
Save mafintosh/8f5d3076a1afd599cbd9bd5289595179 to your computer and use it in GitHub Desktop.
Using shared-structs with workers <3

Using shared-structs with Workers <3

This showcases how you can use shared-structs over an SharedArrayBuffer, to share complex data structures between worker_threads in Node.js

The main.js example starts a worker and passes a shared struct to it.

The worker will periodically update the struct with an incrementing tick and a random number

After 2s it will read the source code of itself into the worker as well.

const { Worker } = require('worker_threads')
const { shared } = require('./structs')
const struct = shared(Buffer.from(new SharedArrayBuffer(shared.bytes)))
const worker = new Worker('./worker.js')
worker.postMessage(struct.rawBuffer.buffer)
setInterval(function () {
console.log('Update:')
console.log('worker.threadId:', struct.threadId)
console.log('worker.ticks:', struct.ticks)
console.log('worker.random:', struct.random)
console.log('worker.src:', struct.buffer.toString('utf-8', 0, struct.bufferLength))
}, 1000)
{
"name": "shared-structs-and-workers",
"version": "0.0.0",
"description": "Using shared-structs with Workers <3",
"main": "main.js",
"scripts": {
"start": "node --experimental-worker main.js"
},
"repository": {
"type": "git",
"url": "git+https://gist.github.com/8f5d3076a1afd599cbd9bd5289595179.git"
},
"author": "Mathias Buus (@mafintosh)",
"license": "MIT",
"bugs": {
"url": "https://gist.github.com/8f5d3076a1afd599cbd9bd5289595179"
},
"homepage": "https://gist.github.com/8f5d3076a1afd599cbd9bd5289595179",
"dependencies": {
"shared-structs": "^1.3.2"
}
}
module.exports = require('shared-structs')(`
struct shared {
int ticks;
double random;
int threadId;
char buffer[5000];
uint32_t bufferLength;
}
`)
const { shared } = require('./structs')
const { parentPort, threadId } = require('worker_threads')
const fs = require('fs')
parentPort.once('message', function (buf) {
const struct = shared(Buffer.from(buf))
struct.threadId = threadId
setInterval(function () {
struct.ticks++
struct.random = Math.random()
}, 250)
setTimeout(function () {
// read some data into the shared struct
fs.open(__filename, 'r', function (err, fd) {
if (err) throw err
fs.read(fd, struct.buffer, 0, struct.buffer.length, 0, function (err, read) {
if (err) throw err
struct.bufferLength = read
})
})
}, 2000)
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment