summaryrefslogtreecommitdiff
path: root/node_modules/webworker-threads/examples/ex05_pool.md
diff options
context:
space:
mode:
authoryo mama <pepper@scannerjammer.com>2015-04-04 01:00:59 -0700
committeryo mama <pepper@scannerjammer.com>2015-04-04 01:00:59 -0700
commitc7c22e3db1c826bcfb2bc66651ec480aae0d4ae0 (patch)
tree8546df448afef40d3814d2581f4dacff7cebb87f /node_modules/webworker-threads/examples/ex05_pool.md
Diffstat (limited to 'node_modules/webworker-threads/examples/ex05_pool.md')
-rw-r--r--node_modules/webworker-threads/examples/ex05_pool.md63
1 files changed, 63 insertions, 0 deletions
diff --git a/node_modules/webworker-threads/examples/ex05_pool.md b/node_modules/webworker-threads/examples/ex05_pool.md
new file mode 100644
index 0000000..528793f
--- /dev/null
+++ b/node_modules/webworker-threads/examples/ex05_pool.md
@@ -0,0 +1,63 @@
+## Using the thread pool
+
+Our previous examples used a single worker thread, and thus only one processor core.
+If we want to take full advantage of multi-core processors, we need the ability to delegate
+expensive computations to a pool of theads. This example demonstrates the pool thread that comes
+bundled with webworker-threads.
+
+First, we create a pool
+
+``` javascript
+var Threads = require('webworker-threads');
+var pool = Threads.createPool(3);
+```
+
+
+Then we load our fibonacci function in all the pool's threads:
+
+``` javascript
+function fibo(n) {
+ return n > 1 ? fibo(n - 1) + fibo(n - 2) : 1;
+}
+
+pool.all.eval(fibo);
+```
+
+Now, we can get fibonacci numbers from our pool
+
+We request them in reverse order, to show that longer computations (`fibo(40)`) run in
+parallel with shorter ones (`fibo(39)`, `fibo(38)`, ...). The results won't come out in strictly decreasing order.
+
+``` javascript
+var remain = 11;
+for (var i = 40; i >= 30; i--) {
+ // extra closure to get proper scoping on 'i'
+ (function(i) {
+ // dispatch each request to the first available thread
+ pool.any.eval('fibo(' + i + ')', function(err, val) {
+ console.log('fibo(' + i + ')=' + i);
+ // destroy the pool when all results have been produced
+ if (--remain == 0) console.log('bye!'), pool.destroy();
+ });
+ })(i);
+}
+```
+
+### Typical (*) Output
+
+(*) Execution is non-deterministic. So order may vary.
+
+```
+fibo(38)=38
+fibo(39)=39
+fibo(37)=37
+fibo(35)=35
+fibo(36)=36
+fibo(33)=33
+fibo(34)=34
+fibo(31)=31
+fibo(32)=32
+fibo(30)=30
+fibo(40)=40
+bye!
+```