I have this script that splits a task between all available CPU threads:
export(int)var count_total = 120000000
export(bool)var multithreading = true
onready var CPU_thread_count = OS.get_processor_count()
var threads = 
CPU_thread_count = 1
var count_per_thread = count_total / CPU_thread_count
for thread_index in range(0, CPU_thread_count):
var thread = Thread.new()
var userdata = [thread_index, count_per_thread]
thread.start(self, "_thread_function", userdata)
print("Thread " + str(thread_index) + " dispatched")
var time_started = OS.get_ticks_msec()
var count = userdata
while(count > 0):
count -= 1
var time_finished = OS.get_ticks_msec()
var process_time = (time_finished - time_started) / 1000.0
print("Thread process time: " + str(process_time))
var thread_index = userdata
func _exit_thread(var thread: Thread):
When I run the script, it takes about 6.5 seconds for a single thread to complete the task.
But when I enable multithreading, it takes about 4 seconds for each thread to complete the task; if we combine those processing times, that's a total 48 seconds of processing!
Is there something I'm doing wrong, here? Shouldn't mutlithreading be faster?
OS: Windows 10
CPU: Intel Core i8700K @ 3.7GHz, 6 cores, 12 threads
GDScript struggles to call two or more functions at the same time. Note that his is an issue with calling the functions; but the logic within the functions can run concurrently. This is true even for Godot 4.0. For efficient multithreaded function calling, you have to use C++. This means using GDNative or a Module. (I use GDNative)
GDScript can perform multithreading efficiently, as long as you avoid concurrent function calling. Doing so is pretty limiting, though.
If your game is slow due to repeated function calling, GDScript multithreading is not the solution. You should first port your code to C++ (which should give you a significant performance boost). Once your code is in C++, then you can benefit from multithreading.
For more information, please read this post: https://github.com/godotengine/godot/issues/58279