Skip to content

Instantly share code, notes, and snippets.

@numtel
Last active February 20, 2018 23:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save numtel/f89a2d9ecc24d0d1fbabf31485049a82 to your computer and use it in GitHub Desktop.
Save numtel/f89a2d9ecc24d0d1fbabf31485049a82 to your computer and use it in GitHub Desktop.
node-raiblocks-pow threaded implementation starting
// 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);
};
}
// 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