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:
- [X] Console in thread
- [X] 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
-
~Context :
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 |
Promise.<*>
thread.join() ⇒ 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
boolean
thread.lock() ⇒ 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
Object
Thread~Context : Kind: inner typedef of Thread
Properties
Name | Type |
---|---|
on | function |
send | function |
terminate | function |
lock | function |
unlock | function |
function
Thread~fnCallback : Kind: inner typedef of Thread
Param | Type | Description |
---|---|---|
...args | args |
Arguments from the Thread constructor |
context | Context |