@snek/threads

0.0.3 • Public • Published

npm David

NPM

threads Version Badge

These are real threads that don't crash and run your js without exploding.

const Thread = require('threads');
const assert = require('assert');

const t = new Thread((a, b) => a + b, 1, 2);

t.join().then((r) => {
  assert(r === 3);
});

TODO:

  • [X] Console in thread
  • [X] Buffer in thread (freoss/buffer)
  • [ ] Allow passing "references" instead of copies

Docs

Thread

Kind: global class

new Thread(fn, ...props)

Create a thread

Param Type Description
fn fnCallback Function that will run in a new thread
...props * Values to pass to the thread callback

thread.send(value)

Send a value to the thread

Kind: instance method of Thread

Param Type Description
value * Value to send

thread.join() ⇒ Promise.<*>

Return a promise that resolves when the thread finishes with the return value or rejects when there is an execution error in the thread.

Kind: instance method of Thread

thread.terminate()

Terminate the thread

Kind: instance method of Thread

thread.lock() ⇒ boolean

Lock the thread's context's mutex, analogous to std::mutex::try_lock

Kind: instance method of Thread
Returns: boolean - If the lock was successfully obtained

thread.unlock()

Unlock the thread context's mutex, analogous to std::mutex::unlock

Kind: instance method of Thread

Thread~Context : Object

Kind: inner typedef of Thread
Properties

Name Type
on function
send function
terminate function
lock function
unlock function

Thread~fnCallback : function

Kind: inner typedef of Thread

Param Type Description
...args args Arguments from the Thread constructor
context Context

Readme

Keywords

none

Package Sidebar

Install

npm i @snek/threads

Weekly Downloads

1

Version

0.0.3

License

MIT

Last publish

Collaborators

  • snek