What is the Status of Multi-threading/Multi-Core/Parallel Processing in Lime/OpenFl?

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)?

Not sure about GPU but in native targets you should be able to use Haxe’s built in threading for splitting CPU tasks.

In flash you’d have to use Workers but it doesn’t look like they’ve been added to OpenFL yet.

I don’t think HTML supports threads but I could be wrong?

So it already there, its just needed to be tapped out. thanks! :slight_smile:

Lime has a few APIs for it:

http://docs.openfl.org/lime/system/BackgroundWorker.html
http://docs.openfl.org/lime/system/ThreadPool.html

It does not use worker on Flash, but this could be added by someone (probably?)

It goes synchronously for systems that don’t support threads, at the moment, but I think this may be a good approach for simple multi-threaded work :wink:

What do you think?

Here’s a simple example of it in use:

Happy to answer any questions :slight_smile:

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.

Unfortunately, this might not be possible at the moment:

https://groups.google.com/forum/#!topic/haxelang/2gJUwheiY-c

OK but what is the utility of the BackgroundWorker.cancel() function then?

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

1 Like