LeichtgewichtLeichtgewicht

2010/04/11 – 02:08

Step based thread processor for libspark

I have been holding a presentation today at the Flash User Group Cologne about Threading and a processor I wrote as addition to the libspark thread framework.

Threads in AS3 you say? Impossible!

Well of course Flash is single-threaded. Never-the-less people are using pseudo thread technology for years using distributed methods on ENTER_FRAME.

It works a that way: A cpu intense method like this

for( var i: int = 0; i<1000000000; ++i );

gets split up in a method for setup method

function setup(): void { _i = 0; } // simplified

and a process method.

function process(): Boolean {
  ++_i;
  return i>=1000000000; // will return true once its finished
}

When its aligned like that the method can be called a certain time per frame, or just until a certain execution time is reached. Then its possible to interrupt execution.

Most persons that implement a asynchronous mechanism like that tend to forget or ignore that one of this pseudo threads at a time is a simple deal, but dealing with more than one has all the problems of traditional threads additionally to the fact that this strategy will most certainly end in juddering once two process try to limit their time to the max possible of a frame.

This leads me to implement one generic Processor and abstract tools that make it simple and relatively high performant to create such CPU-intense methods.

Libspark Thread

Libspark's Thread library is for me right now the most advanced thread library in flash. It does abstract a lot of basic, time-consuming tasks, like

LoaderThread ... to load files to the loader
URLLoaderThread ... to load files normally with a URLLoader
TweenerThread ... Wrapper for Tweener animations
Frocessing2dThread ... Wrapper for the Processing AS3 port called Frocessing

which are useful, no doubt. But a proper step based processing is still left out missing.

Thats why I created a add-on for the Thread library that has offers a bit more than the usual few bit named:

ThreadStepProcessor

At the very core is the abstract class StepThread. For your own time consuming threads you don't have to do a lot more than extend this class and implement the init and process methods.

class MyThread extends StepThread {
   override protected function init(): { /* start up condition */ }
   override protected function process(): Boolean { /* one step */ }
}

This thread will already be fully integrated in the rest of the libspark Thread system. If you start such a StepThread it will automatically add itself to the StepProcessor.

The processor will align all parallel started threads and take best care that a certain amount of milliseconds per frame is not exceeded. By default its set to 25 milliseconds. It can be changed to your wishes using

StepProcessor.MAX_EXECUTION_TIME_PER_FRAME_IN_MS = 60; // as a example 

A major problem of parallel executing threads is a so-called race condition. A solution to this is object locking. The StepThread has a rudimentary but well working implementation ready with:

lockTo( objectsThatThisThreadWillUse );

a thread can lock certain objects. If another thread wants to lock as well the other thread will be put on hold until the first one finished. All locks will be released on finish.

As long as Flash is single-threaded and we have to deal with it, a central processor might very well be the best suited solution.

Download & Sources

Since this framework is closely connected to libspark Thread it can be found in the wiki:
Libspark ThreadStepProcessor

Enjoy!

Tags: , , , , ,

Comments

  1. CookingNo Gravatar

    Woah! I’m really digging the template/theme of this website. It’s simρle, yet effеctive.

    A lot οf times it’s difficult to get that “perfect balance” between usability and visual appeal. I must say that you’νе dοne a amazing
    job with thіs. Aԁditionally, the blog loаds ѵeгy
    quіck for me on Inteгnet еxρlorer.
    Excеllеnt Βlog!

Leave a Reply

Site informations

Martin Heidegger – Web Developerskype:mastakanedaxing:martin.heideggertwitter:leichtgewicht