Last active
February 20, 2018 23:08
-
-
Save numtel/f89a2d9ecc24d0d1fbabf31485049a82 to your computer and use it in GitHub Desktop.
node-raiblocks-pow threaded implementation starting
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Change index.js to this contents | |
var cp = require('child_process'); | |
var os = require('os'); | |
var addon = require('bindings')('functions.node'); | |
function zeroPad(num, size) { | |
// Max 32 digits | |
var s = "00000000000000000000000000000000" + num; | |
return s.substr(s.length-size); | |
}; | |
module.exports = function(hex) { | |
return zeroPad(addon.calculate(hex), 16); | |
} | |
module.exports.threaded = function(hex, cb) { | |
var threads = []; | |
var threadCount = os.cpus().length; | |
for(var i =0; i < threadCount; i++) { | |
var thread = cp.fork(__dirname + '/thread.js'); | |
thread.on('message', work => { | |
for(var j =0; j< threads.length; j++) { | |
cp.spawn('taskkill', ['/pid', threads[j].pid, '/f', '/t']); | |
} | |
cb(zeroPad(work, 16)); | |
}); | |
thread.send(hex); | |
}; | |
} | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Create a new file, thread.js | |
var addon = require('bindings')('functions.node') | |
process.on('message', hex => { | |
process.send(addon.calculate(hex)); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment