0.0.3 • Published 7 years ago

@snek/threads v0.0.3

Weekly downloads
1
License
MIT
Repository
github
Last release
7 years ago

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:

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

Docs

Thread

Kind: global class

new Thread(fn, ...props)

Create a thread

ParamTypeDescription
fnfnCallbackFunction 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

ParamTypeDescription
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

NameType
onfunction
sendfunction
terminatefunction
lockfunction
unlockfunction

Thread~fnCallback : function

Kind: inner typedef of Thread

ParamTypeDescription
...argsargsArguments from the Thread constructor
contextContext