This library is currently under development, thus do not use it in production.
- core
- custom exceptions
- make
while
andfor
non-blocking in shim workers - improve sandboxing shim workers
- websockets
- indexeddb
- xhr
- imagedata
Threadr uses Webworkers when present, otherwise it falls back to a singlethreaded application which still tries to be non-blocking by making only asynchronous requests. So Threadr will try to find the most performant way to execute your code that is possible in the users browser, so you don't have to worry about different implementation. Threadr event lets you use Websockets, IndexedDB, ImageData and spawn sub-threads inside a thread.
Threadr aims to boost performance whereever it is possible.
Example: HTML5 game Your rendering engine runs on the main thread, while the physics engine and the networking engine run in different Threadr-threads. Threadr will use multi-threading in browsers that support Webworkers, which means, that physics and network won't block your rendering, so your rendering can now take up to 16.6ms and still runs smoothly at 60fps. When the users browser does not support webworkers it will just fallback to a single-threaded application without requiring you to write multiple code fragments.
Threadr's core engine supports IE8+, Chrome 1+, Firefox 6+, Opera 12.1+, Safari 4+ but will run faster in IE10+, Chrome 13+, Firefox 8+, Opera 12.1+, Safari 5.1+ since these support Webworkers natively.
Threadr enables you to use Websockets/IndexedDB inside threads which is supported in browsers that support Websockets/IndexedDB.
<div id="prime"></div>
<script src="path/to/threadr.js" type="text/javascript"></script>
<script>
//configure threadr
Threadr.config( "path/to/threadr.runnable.js" );
//spawn a thread
var thread = Threadr.spawn( function( ) {
var n = startAt,
count = 0;
setInterval( function( ) { // while( true ) {
n++;
for( var i = 2; i <= Math.sqrt(n); i++ ) {
if( n%i === 0 ) {
return; // continue;
}
}
post( "prime", [n, ++count] );
}, 1); // }
} );
// assign event handler
thread.on( "prime", function( n, c ) {
document.getElementById( "prime" ).textContent = "Latest Prime found: " + n + " (" + c + "nth prime)";
} );
// start thread execution
thread.start( { startAt: 1 } );
</script>
This object is available in the main thread and in subthreads
Threadr.spawn(code)
returnsThread
Function to create a new Thread-objectcode
{function
} code that will be executed by the thread
Threadr.url
only available on the main thread {string
} relative or absolute path torunnable.js
Threadr.count
{number
} Count currently active threadsThreadr.supports
{object
} Object storing API support information about the current environment (user browser)workers
{boolean
} Webworker supportwebsockets
{boolean
} Websocket supportindexeddb
{boolean
} indexedDB support
This object is created using Threadr.spawn()
Thread#start(params)
returnsthis
Start thread executionparams
{object
} optional parameters to be available to the thread (needs JSON compatiblity)
Thread#terminate()
returnsthis
Kills the thread, terminated threads can not be restartedThread#post(name, args)
returnsthis
Function to post a message to the thread, triggering thename
-eventname
{string
} event nameargs
{array
} optional aruments passed to the event handlers
Thread#on(name, handler)
returnsthis
Function to add a event handler, listening to events coming from the threadname
{string
} event namehandler
{function
} event handler function
Thread#off(name, handler)
returnsthis
Function to remove a previously assigned event handlername
{string
} event namehandler
{function
} event handler function
Thread#running
{boolean
} Check whether the thread is running
Functions accessable from inside a thread
load(scripts,callback)
Function to load dependend javascript filesscripts
{string
|array
} path or array of paths to javascript files to be loaded.callback
{function
} triggered when all files have been loaded
terminate()
Terminate the thread, terminated threads can not be restartedThread#post(name, args)
returnsthis
Function to post a message to the parent thread, triggering thename
-eventname
{string
} event nameargs
{array
} optional aruments passed to the event handlers
Thread#on(name, handler)
returnsthis
Function to add a event handler, listening to events coming from the parent threadname
{string
} event namehandler
{function
} event handler function
Thread#off(name, handler)
returnsthis
Function to remove a previously assigned event handlername
{string
} event namehandler
{function
} event handler function
Furthermore you have access to the following global functions:
setInterval
setTimeout
clearInterval
clearTimeout
atob
btoa
And of course to Threadr
to spawn subthreads.