Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

@esfx/threading-spinwait

Package Overview
Dependencies
Maintainers
1
Versions
40
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@esfx/threading-spinwait

Provides 'SpinWait', a thread synchronization primitive for use with Workers.

  • 1.0.0
  • latest
  • Source
  • npm
  • Socket score

Version published
Maintainers
1
Created
Source

@esfx/threading-spinwait

Provides SpinWait, a thread synchronization primitive for use with Workers.

Overview

  • Installation
  • Usage
  • API

Installation

npm i @esfx/threading-spinwait

Usage

import { Worker, isMainThread, parentPort, workerData } from "worker_threads";
import { SpinWait } from "@esfx/threading-spinwait";
import { StructType, int32 } from "@esfx/struct-type";

const SharedData = StructType([
    { name: "ready", type: int32 },
]);

function worker_thread() {
    const data = new SharedData(workerData);

    // do some long running process...

    // signal that the work has completed.
    data.ready = 1;
}

function main() {
    const data = new SharedData(/*shared*/ true);
    const worker = new Worker(__filename, { workerData: data.buffer });

    // start spinning until the condition is met.
    // this results in the thread sleeping periodically
    // while it waits for the condition.
    const spinWait = new SpinWait();
    spinWait.spinUntil(() => data.ready === 1);
}

API

You can read more about the API here.

FAQs

Package last updated on 20 Oct 2022

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc