10.12.1 Threading

since Haxe 4.0.0

A unified threading API is available on some sys targets. The compile-time define target.threaded is set when the API is available. The API allows very simple creation of threads from functions:

class Main {
  public static function main():Void {
#if (target.threaded)
    sys.thread.Thread.create(() -> {
        while (true) {
          trace("other thread");
          Sys.sleep(1);
        }
      });
    Sys.sleep(3);
#end
  }
}

All spawned threads are treated as daemon threads, meaning that the main thread will not wait for their completion.

Due to threads having access to a shared memory space with all the Haxe variables and objects, it is possible to run into issues due to deadlocks and race conditions. The standard library provides some core synchronization constructs in the sys.thread package.