day-10.mjs
January 3, 2022 ยท View on GitHub
/////////////////////////////////////////////////////////////////////////////// // Day 10 - Back to Algorithms // // #region problem // /////////////////////////////////////////////////////////////////////////////// // Write script that stops the main thread while a worker performs a // // setTimeout of 100ms. // ///////////////////////////////////////////////////////////////////////////////
/**
- This needs to pause the thread while waiting on the worker before
- returning.
- @param {import('node:worker_threads').Worker} worker / function onMainThread(worker) { // ... pause main thread while worker setTimeout runs ... } /*
- This needs to cause the main thread to resume. */ function onWorkerThreadTimeout() { // ... resume main thread ... }
/////////////////////////////////////////////////////////////////////////////// // #endregion problem // ///////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////// // Test - Leave code after this alone // // #region test // ///////////////////////////////////////////////////////////////////////////////
import assert from 'node:assert/strict'; import worker_threads from 'node:worker_threads';
if (worker_threads.isMainThread === true) { const sabArr = new Int32Array(new SharedArrayBuffer(4)); const worker = new worker_threads.Worker( new URL(import.meta.url), { workerData: sabArr } ); process.exitCode = 1; onMainThread(worker); process.exitCode = 0; assert.strictEqual(sabArr[0], 1); } else { setTimeout(() => { worker_threads.workerData[0] = 1; onWorkerThreadTimeout(); process.exit(); }, 100); }
/////////////////////////////////////////////////////////////////////////////// // #endregion test // ///////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////// // Hints - Encoded in ROT13 encoded // // #region hints // /////////////////////////////////////////////////////////////////////////////// // // // Fvzcyr: Lbh arrq gb hfr Ngbzvpf.jnvg naq flapuebabhfyl ernqvat n zrffntr // // ba n ZrffntrCbeg. // // // /////////////////////////////////////////////////////////////////////////////// // #endregion hints // ///////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////// // Use Cases // // #region uses // /////////////////////////////////////////////////////////////////////////////// // // // This workflow can allow blocking APIs to use async workflows without // // needing a breaking change. // // // /////////////////////////////////////////////////////////////////////////////// // #endregion uses // ///////////////////////////////////////////////////////////////////////////////