Godot Version
4.5.1
Question
I am working on an app that uses a lot of multithreading for its primary use case.
The user can start the ‘task’ with an arbitrary number of threads, which involves having each thread run a simple script where it looks at a folder on the users file system and reads the folders and files and returns data representing it.
(Dumbed-down equivalent for examples sake)
func search(folder : String) -> PackedStringArray:
var results : PackedStringArray = []
results.append(DirAccess.get_files_at(folder))
results.append(DirAccess.get_directories_at(folder))
return results
This works fine, the issue is since it relies on a lot of reading from disk, if multiple large directories are read from at once the read speed on the disk / drive gets maxed out and therefor each thread will be waiting until it gets given its information.
So if the user then tries to stop the process (Which I allow them to do without closing the app) then they need to wait for each one to finish, which will take an unpredictable amount of time.
(How this is currently done)
# 'all_threads_idle' returns true if every thread in 'threads'
# is not started
# It is inside a while to make sure it only continues if
# all threads are finished properly.
while not all_threads_idle:
for t : Thread in threads:
if t.is_started():
while t.is_alive():
await get_tree().process_frame
t.wait_to_finish()
If I just try to wait for each thread to finish the app will hang instead of displaying the loading screen I show before and while running this, and if I try to free the threads without finishing them I get warnings about not properly closing the threads.
Leaving me to wait for the threads to finish despite not using the return values as the task is being abandoned.
Is there a way to safely stop the threads without waiting (Atleast waiting as long)?
The Godot editor is seemingly able to do this, as if you force-close the app during this (On any time while threads are active) no warnings appear about thread leaks / issues in the console and the app is closed near-immediately, so there seems to be a way to safely force-stop a thread quickly before freeing it; how can this be done manually without just closing / crashing the app?
Thanks.