Why thread.wait_to_finish() freezes app on Android devices?

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By VanDeiMin

if I use threads, app freezes when executing thread.wait_to_finish(). It happens only on android devices. On desktop everything works just fine.

:bust_in_silhouette: Reply From: godot_dev_

Could be a race condition, where your desktop has better specifications than your Android device and so the race condition never creates a deadlock on your desktop but does on the Android.

Try adding random CPU lag and see if you can recreate the situation where thread.waittofinish() blocks on your desktop

I think this explanation is quite reasonable. It is possible that the problem is due to lack of compatibility, you can test this problem on similar compatible devices.


we become what we behold

dabbygentle | 2022-12-16 03:21

Hi.
Thank You for the answer.

How can I add random lag?

My desktop: Wacom Mobile Studio on intel i7 with nvidia quadro m1000m
Androids I’ve tried: Samsung J6, A12, A50.

VanDeiMin | 2022-12-20 18:18

:bust_in_silhouette: Reply From: bsmxofficial

The Thread may be still running when the wait_to_finish function called, this can block the calling thread. You have to wait for the function completion and then call wait_to_finish. I had the same issue and this code work’s fine:

#e.g. you can join the thread when the Node exit the tree.
func _exit_tree():
	if thread.is_alive() == false:
        #print the return value from function called in the thread.
		print(thread.wait_to_finish())

There is some interesting information in Docs about that: See the Godot’s API Class Reference