haxe-threadpool icon indicating copy to clipboard operation
haxe-threadpool copied to clipboard

Thread pool library for haxe

Thread pool library for haxe (neko, CPP, java, CS or python)

MIT License TravisCI Build Status AppVeyor Build Status

git clone https://github.com/thomasuster/haxe-threadpool.git
haxelib dev haxe-threadpool haxe-threadpool

addConcurrent example

var pool = new ThreadPool(4);
var didWork = false;
var work = function(t:Int) {
    didWork = true;
};
pool.addConcurrent(work);
pool.blockRunAll();
assertTrue(didWork);
pool.end();

distributeLoop example

var pool = new ThreadPool(4);
var source:Array<Int> = [10,20,30];
var copy:Array<Int> = [0,0,0];
pool.distributeLoop(source.length,function(t:Int, index:Int) {
    copy[index] = source[index];
});
pool.blockRunAll();
assertEquals(source.join(','), '10,20,30');
pool.end();

Shared resources example

var pool = new ThreadPool(4);
var sumM:Mutex = new Mutex();
var sum:Int = 0;
var nums:Array<Int> = [10,20,30];
pool.distributeLoop(nums.length,function(t:Int, index:Int) {
    sumM.acquire();
    sum+=nums[index];
    sumM.release();
});
pool.blockRunAll();
assertEquals(10+20+30, sum);
pool.end();

Thread resource example

var pool = new ThreadPool(2);
var threadNames = ["Tom", "Jerry"];
pool.addWork(function(t:Int) {
    Sys.println("${threadNames[t]} did the work.");
});
pool.blockRunAll();
pool.end();