I'm working on a piece of image processing software, and I'd like to implement some multi threading. I've already got the processing on one thread, and the UI updates on another so that the whole thing doesn't freeze when processing an image. However, I'd like to split the processing thread up even further so it can work on multiple parts of the image at the same time.
In order to do that, I wanted to see how many threads I could use. OS.getprocessorcount() returns how many threads the system can support, but surely some of those might be in use by other programs? So I wrote a little script to see how many threads I could access at once.
print("%s - processor count %s"%[Time.get_ticks_msec(),OS.get_processor_count()])
var i = 0
var thrds = 
while i < 100:
var er = thrds[thrds.size()-1].start(do_thing)
if er != OK:
i += 1
print("%s - useable threads: %s"%[Time.get_ticks_msec(),i])
#old do_thing: wait 100 secs
#new do_thing: get prime numbers in a long/stupid way to eat up time/pocessing power
var MAX = 5000
var prime_numbers = 
for i in range(2,MAX+1):
var p = true
for ii in range(i-1,1,-1):
if i%ii == 0:
p = false
print("%s - Ended: %s"%[Time.get_ticks_msec(),OS.get_thread_caller_id()])
It makes an empty array, adds a thread to the array, asks the thread to do a task, then adds +1 to the thread count. It does this until Thread.new() does not return OK.
But it always returns OK.
OS.getprocessorcount() returns 8 on my system, but I've run this code up to 500 threads, and it makes them. My computer slows to a crawl, basically unusable, and Godot will still make new threads.
- How can I find out how many threads I can/should use without slowing the system?
- Can I just use OS.getprocessorcount()? Should I use less to make sure I'm not going to bog down the system?
- Does Thread.start() always return OK? The documentation says it should return ERRCANTCREATE if it can't make a new thread, but I've asked it to make 100,000 threads(with the old do_thing) and it always returned OK. Ask for 1,000,000 though and my computer freezes and crashes... presumable because Godot always said OK to making more threads.