Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Thread is not working in WebAssembly export #12699

Closed
stubbsy345 opened this issue Nov 6, 2017 · 19 comments
Closed

Thread is not working in WebAssembly export #12699

stubbsy345 opened this issue Nov 6, 2017 · 19 comments

Comments

@stubbsy345
Copy link

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.

@akien-mga akien-mga changed the title Thread is not working in godot 3.0 alpha 2 WebAssembly Thread is not working in WebAssembly export Nov 6, 2017
@akien-mga
Copy link
Member

cc @eska014

@leonkrause
Copy link
Contributor

Threads are not in the WASM spec yet, work is ongoing here: https://github.com/webassembly/threads

@leonkrause leonkrause added this to the 3.1 milestone Nov 6, 2017
@stubbsy345
Copy link
Author

Is there any way of getting similar functionality to threading. I'm trying to avoid HTTP requests from blocking the main thread?

@reduz
Copy link
Member

reduz commented Nov 6, 2017 via email

@Zylann
Copy link
Contributor

Zylann commented Nov 6, 2017

In the meantime, I'm pretty sure I've seen a way to use HttpClient without blocking the main thread. Then you woudln't need to use a thread. The doc example shows how to do it with polling: http://docs.godotengine.org/en/latest/learning/features/networking/http_client_class.html?highlight=HttpClient
Except you would use _process instead of a while loop and a sleep ;)

@leonkrause leonkrause removed this from the 3.1 milestone Feb 26, 2018
@blockspacer
Copy link

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 emscripten-core/emscripten#7535
Very usefull to read: emscripten-core/emscripten#7535 and emscripten-core/emscripten#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!

@KoBeWi
Copy link
Member

KoBeWi commented Nov 22, 2020

Can anyone still reproduce this bug in Godot 3.2.3 or any later release?

@Calinou
Copy link
Member

Calinou commented Nov 22, 2020

@KoBeWi Yes, as HTML5 thread support isn't implemented in a stable Godot release yet. Browser support for WebAssembly threads is still not fully reliable yet either, especially on mobile platforms.

@akien-mga akien-mga added this to the 4.0 milestone Nov 22, 2020
@akien-mga
Copy link
Member

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
Copy link

lenscas commented Dec 1, 2020

I just gave https://godotengine.org/article/dev-snapshot-godot-3-2-4-beta-3 a try to see how it works and I'm not sure how to enable threads?

I don't see an option for it in the export though. Do I need to do something special, am I blind or something else?
image

@Faless
Copy link
Collaborator

Faless commented Dec 1, 2020

@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

@blackears
Copy link

I'm using stable 3.4 and still running into the same issue. Threads for web are not enabled automatically and I cannot find any option for it in the export options either.

@Calinou
Copy link
Member

Calinou commented Jun 28, 2022

I'm using stable 3.4 and still running into the same issue. Threads for web are not enabled automatically and I cannot find any option for it in the export options either.

Threads still need to be enabled on export by changing Export Type in the HTML5 export preset:

image

See Exporting for the web as for why HTML5 threads are disabled by default.

Using both threads and GDNative at the same time may be supported in a future release, but 3.4.x doesn't support that.

@blackears
Copy link

When I select that option, I am given an error that a necessary export template is not available. I opened the Manage Export Templates window, but the webassembly_threads is not there. I did download what was presented, but it did not make this error message go away.

image

@akien-mga
Copy link
Member

akien-mga commented Jun 28, 2022

Mono templates can't be built with threading support sadly. It's available for non-Mono builds. Likewise for GDNative support

Also, thus issue dates back to before threading support was even implemented. Please open new issues when so many years have passed.

@blackears
Copy link

I came to this thread from #34506 which was referencing this as a solution for C# threading issues. I'm basically just looking for a way to use async/await in C# and export something that works for web.

@Faless
Copy link
Collaborator

Faless commented Jun 28, 2022

@blackears as I mentioned in #34506 (comment) the mono version do not support threads, because the mono version we use do not support modern emscripten versions.
Godot 4 should should come with donet6 which should support modern emscripten versions (and thus threading).

@ashelleyPurdue
Copy link

@blackears as I mentioned in #34506 (comment) the mono version do not support threads, because the mono version we use do not support modern emscripten versions. Godot 4 should should come with donet6 which should support modern emscripten versions (and thus threading).

Why does Godot offer an option if that option isn't real?

@Zireael07
Copy link
Contributor

@ashelleyPurdue There are a LOT of things that do NOT work in some combination of export platform/renderers

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests