Skip to content

Instantly share code, notes, and snippets.

@jthomas
Last active April 10, 2022 13:22

Revisions

  1. jthomas revised this gist May 8, 2019. 1 changed file with 0 additions and 12 deletions.
    12 changes: 0 additions & 12 deletions package.json
    Original file line number Diff line number Diff line change
    @@ -1,12 +0,0 @@
    {
    "name": "worker_threads",
    "version": "1.0.0",
    "description": "",
    "main": "primes-with-workers.js",
    "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
    },
    "keywords": [],
    "author": "James Thomas <[email protected]> (https://jamesthom.as)",
    "license": "MIT"
    }
  2. jthomas created this gist May 8, 2019.
    12 changes: 12 additions & 0 deletions package.json
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,12 @@
    {
    "name": "worker_threads",
    "version": "1.0.0",
    "description": "",
    "main": "primes-with-workers.js",
    "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
    },
    "keywords": [],
    "author": "James Thomas <[email protected]> (https://jamesthom.as)",
    "license": "MIT"
    }
    41 changes: 41 additions & 0 deletions primes-with-workers.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,41 @@
    'use strict';

    const { Worker } = require('worker_threads');
    const os = require('os')
    const threadCount = os.cpus().length

    const compute_primes = async (start, range) => {
    return new Promise((resolve, reject) => {
    let primes = []
    console.log(`adding worker (${start} => ${start + range})`)
    const worker = new Worker('./worker.js', { workerData: { start, range }})

    worker.on('error', reject)
    worker.on('exit', () => resolve(primes))
    worker.on('message', msg => {
    primes = primes.concat(msg)
    })
    })
    }

    async function main(params) {
    const { min, max } = params
    const range = Math.ceil((max - min) / threadCount)
    let start = min < 2 ? 2 : min
    const workers = []

    console.log(`Calculating primes with ${threadCount} threads...`);

    for (let i = 0; i < threadCount - 1; i++) {
    const myStart = start
    workers.push(compute_primes(myStart, range))
    start += range
    }

    workers.push(compute_primes(start, max - start))

    const primes = await Promise.all(workers)
    return { primes: primes.flat() }
    }

    exports.main = main
    24 changes: 24 additions & 0 deletions primes.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,24 @@
    'use strict';

    const min = 2

    function main(params) {
    const { start, end } = params
    console.log(params)
    const primes = []
    let isPrime = true;
    for (let i = start; i < end; i++) {
    for (let j = min; j < Math.sqrt(end); j++) {
    if (i !== j && i%j === 0) {
    isPrime = false;
    break;
    }
    }
    if (isPrime) {
    primes.push(i);
    }
    isPrime = true;
    }

    return { primes }
    }
    27 changes: 27 additions & 0 deletions workers.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,27 @@
    'use strict';
    const { Worker, isMainThread, parentPort, workerData } = require('worker_threads');

    const min = 2

    function generatePrimes(start, range) {
    const primes = []
    let isPrime = true;
    let end = start + range;
    for (let i = start; i < end; i++) {
    for (let j = min; j < Math.sqrt(end); j++) {
    if (i !== j && i%j === 0) {
    isPrime = false;
    break;
    }
    }
    if (isPrime) {
    primes.push(i);
    }
    isPrime = true;
    }

    return primes
    }

    const primes = generatePrimes(workerData.start, workerData.range);
    parentPort.postMessage(primes)