godotengine / godot-proposals

Godot Improvement Proposals (GIPs)
MIT License
1.12k stars 69 forks source link

Detachable threads #769

Closed Vlad-Zumer closed 4 years ago

Vlad-Zumer commented 4 years ago

Describe the project you are working on: I am working on a rogue lite with procedural generated loot, akin to POE Describe the problem or limitation you are having in your project: Saving events slows down the main thread too much, but the thread implementation requires the call of wait and join. Describe the feature / enhancement and how it helps to overcome the problem or limitation: Implementing detachable threads that free memory as soon as they finish execution, so you'd be able to "fire and forget" an event. Describe how your proposal will work, with code, pseudocode, mockups, and/or diagrams: Thread.start ( Object instance, String method, Variant userdata=null, int priority=1, detached=false ) If this enhancement will not be used often, can it be worked around with a few lines of script?: I have yet to find a solution. Is there a reason why this should be core and not an add-on in the asset library?: It would be easier to implement in the core by using the already available API

Possible fix: https://github.com/godotengine/godot/pull/34862 Possible Help: find where the thread libraries are used and how.

Xrayez commented 4 years ago

I have to note that godotengine/godot#34862 works on the script level only and doesn't really allow for "auto-wait/join", but certainly simplifies the process, also yield(thread, "completed"). I'm afraid to dig deeper because of #7: Solutions must be local. 🙂

There was a discussion regarding removing the need for wait_to_finish() altogether, go through links starting from https://github.com/godotengine/godot/pull/28457#issuecomment-487225942 for more details.

Vlad-Zumer commented 4 years ago

I think if we emit a complete signal it would fix this problem in particular. And would allow for the creation of auto-merged threads directly in GD script by using something like this:

extends Thread

class_name AutoMergedThread

func _init().():
    .connect("completed", self, "_on_thread_completed");

func _on_thread_completed(_aux):
    .wait_to_finish();

There may still be an argument for detachable threads in the future, for better general purpose programming but right now I think it breaks #2 The problem has to exist. I will close this issue for now but please open it again if you deem it necessary.