From c7c22e3db1c826bcfb2bc66651ec480aae0d4ae0 Mon Sep 17 00:00:00 2001 From: yo mama Date: Sat, 4 Apr 2015 01:00:59 -0700 Subject: first --- .../examples/quickIntro_multiThreadEvented.js | 30 ++++++++++++++++++++++ 1 file changed, 30 insertions(+) create mode 100644 node_modules/webworker-threads/examples/quickIntro_multiThreadEvented.js (limited to 'node_modules/webworker-threads/examples/quickIntro_multiThreadEvented.js') diff --git a/node_modules/webworker-threads/examples/quickIntro_multiThreadEvented.js b/node_modules/webworker-threads/examples/quickIntro_multiThreadEvented.js new file mode 100644 index 0000000..bd7f4c1 --- /dev/null +++ b/node_modules/webworker-threads/examples/quickIntro_multiThreadEvented.js @@ -0,0 +1,30 @@ +var numThreads= 10; +var threadPool= require('webworker-threads').createPool(numThreads); +threadPool.load(__dirname + '/quickIntro_evented_childThreadCode.js'); + +/* + This is the code that's .load()ed into the child/background threads: + + function fibo (n) { + return n > 1 ? fibo(n - 1) + fibo(n - 2) : 1; + } + + thread.on('giveMeTheFibo', function onGiveMeTheFibo (data) { + this.emit('theFiboIs', fibo(+data)); //Emits 'theFiboIs' in the parent/main thread. + }); + +*/ + +//Emit 'giveMeTheFibo' in all the child/background threads. +threadPool.all.emit('giveMeTheFibo', 35); + +//Listener for the 'theFiboIs' events emitted by the child/background thread. +threadPool.on('theFiboIs', function cb (data) { + process.stdout.write(" ["+ this.id+ "]"+ data); + this.emit('giveMeTheFibo', 35); +}); + +(function spinForever () { + process.stdout.write("."); + process.nextTick(spinForever); +})(); -- cgit v1.2.3-70-g09d2