When I am doing flash, it was really difficult to do Multi-threading/Multi-Core/Parallel processing.
What is the status of support for Multi-threading/Multi-Core/Parallel processing that Lime/OpenFL could take advantage of from Multi-core CPU (and/or Multi-core GPU)?
I just tried to use lime BackgroundWorker with openfl. It works fine exept that when I call worker.cancel(), the thread function defined with doWork continue in the background. It just seems to be detached from the main openfl thread (the onComplete function is not called at the end of the worker work function if I have called worker.cancel() before it ends).
Indeed, I have made a popup with a “cancel” button that call worker.cancel() when clicked. And in my worker.dowork function, that contains a loop, I have put a trace call. If I click my cancel button, I still can see trace output from my dowork funtion appearing in the console. So the function is actually not canceled. It is just running (and consuming some CPU) for nothing (as the result won’t be passed to the main openfl script anyway).
So how should I properly stop a BackgroundWorker task before it ends by itself?
PS: I tested on windows target, not on android yet but the behavior is probably the same on any cpp target.
It sets the canceled boolean of the worker, you could check this value within a loop in your doWork to check for canceled status and exit gracefully
For example, something like this
var worker = new BackgroundWorker ();
var work = function (_) {
var i = 0;
while (i < 100 && !worker.canceled) {
i++;
worker.sendProgress (i);
}
worker.sendComplete (i);
};
worker.doWork = work;
worker.run ();
well you could just do it with a boolean var defined outside of the doWork function. No need to have a canceled var attached to the worker for that, so this cancel() function seems a bit useless for now (until it can really cancel the thread). But thanks.
In the future, if there is some way to kill a Haxe thread safely, this is where we will add it. In the meantime, if you use a worker to do something where you don’t have control over stopping it halfway, the cancel will at least ignore the result, so it behaves like it was killed, even if you don’t free up the resources as well