• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1// Flags: --expose-gc
2'use strict';
3
4const common = require('../common');
5const assert = require('assert');
6const { Worker } = require('worker_threads');
7
8{
9  const sharedArrayBuffer = new SharedArrayBuffer(12);
10  const local = Buffer.from(sharedArrayBuffer);
11
12  const w = new Worker(`
13    const { parentPort, workerData } = require('worker_threads');
14    const local = Buffer.from(workerData.sharedArrayBuffer);
15
16    parentPort.on('message', () => {
17      local.write('world!', 6);
18      parentPort.postMessage('written!');
19    });
20  `, {
21    eval: true,
22    workerData: { sharedArrayBuffer }
23  });
24  w.on('message', common.mustCall(() => {
25    assert.strictEqual(local.toString(), 'Hello world!');
26    global.gc();
27    w.terminate();
28  }));
29  w.postMessage({});
30  // This would be a race condition if the memory regions were overlapping
31  local.write('Hello ');
32}
33