Creating thread in script. BUG?


#1

As the title says I was not sure if this is a bug because of my lack of knowledge.
This is the simple script for the purpose of demonstration.

package arm;
import cpp.vm.Thread;

class ThreadTrait extends iron.Trait {
var countingThread: Thread;
public function new() {
super();

	notifyOnInit(function() {
		countingThread = Thread.create(counting);
	});
}

function counting(){
	var count = 0;
	while(count < 500){
		count++;
		trace(count);
	}
}

}

Program just randomly exits. Thread is spawned and it prints count variable but not to the end of while loop. Sometimes prints to count=350 or smaller value. When I open project in Visual Studio and start to debugging in most cases program is working. Here is the screenshot of error in VS I have managed to catch
vserror


#2

I’ve never done threading myself in Armory or Haxe, but you might try using Kha’s Worker API. From this post:

Yes, you can use kha.Worker to write multithreaded code that runs via HTML5 and natively. Keep in mind that native targets share memory but HTML5 does not allow that so you need to be a little careful to keep your code portable.

You may not be interested in porting to HTML5, but maybe Kha’s native threading implementation will work for you. I don’t actually know how to use the Worker, though, so I don’t have a practical example with it.