godot: Thread is not working in WebAssembly export

Operating system or device, Godot version, GPU Model and driver (if graphics related): Godot alpha 2 Webassembly export.

Thread does not work in WebAssembly, I am conscious that may be because it is not possible to thread on this medium. But can’t find anything stating that.

var thread = Thread.new()
func start_order(ord):
	thread.start(self, "add_order", ord)
	
func add_order(ord):
	ordered = {"ReturnTime": ord["returnTime"], "WaitLeft": ord["returnTime"]}
	print(ordered)
	thread.wait_to_finish()

Works fine when not using a thread in webassembly.

About this issue

  • Original URL
  • State: closed
  • Created 7 years ago
  • Comments: 19 (10 by maintainers)

Most upvoted comments

Well this is fixed, threads are supported and working in HTML5 with a custom build (threads=yes).

3.2.4 will probably ship with both thread and no-thread templates to select at export.

@lenscas as @akien-mga mentioned, thread support requires a custom build for now. The option has not yet been added to the export UI, but assuming you compiled a template with threads_enabled=yes and you use that as a custom template it will work. The stable 3.2.4 will likely ship both templates, and add the option to the export dialog

Modern Chrome supports threads & “Shared Array Buffer” by default ( https://developers.google.com/web/updates/2018/10/wasm-threads ) For other browsers: you can enable threads with flags / browser settings. See also “Shared Array Buffer” support ( https://caniuse.com/#feat=sharedarraybuffer ) You can enable threads with USE_PTHREADS emscripten flag ( https://emscripten.org/docs/porting/pthreads.html ) and detect threads support at runtime (need to add 2 separate build modes: with and without threads, as fallback).

NOTE: when using USE_PTHREADS don`t block main loop, prefer emscripten_run_in_main_loop/thread https://github.com/emscripten-core/emscripten/issues/7535 Very usefull to read: https://github.com/emscripten-core/emscripten/issues/7535 and https://github.com/emscripten-core/emscripten/issues/8325

  # avoid having main thread synchronously block on a pthread
  # (the ".. sync blocking on main thread is bad.." mantra),
  # so dropping the pthread_join()
  # (and ensuring NO_EXIT_RUNTIME so runtime stays alive)
  # should also avoid the hang
  # DOM operations can only be done on the main thread
  # That includes things like printing to the console (!!!),
  # and even things like XMLHttpRequests!