0.0.3 • Published 7 years ago
@snek/threads v0.0.3
threads
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
- Thread
- new Thread(fn, ...props)
- instance
- .send(value)
- .join() ⇒ Promise.<*>
- .terminate()
- .lock() ⇒ boolean
- .unlock()
- inner
- ~Context : Object
- ~fnCallback : function
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 |