Hi all, Relatively new to nim, very new to multithreading in nim. I'm attempting to run a program on a bunch of files I have the file paths for. The relevant code at the moment is the following:
var t1 : seq[Thread[(string,string,string,uint16,uint16)]]
for file in files:
while true:
if t1.len < int(thread_num):
t1.add(Thread[(string,string,string,uint16,uint16)]())
createThread(t1[t1.len - 1], myFunction, (file, ... <other stuff>))
break
else:
var added_flag = false
for i,thr in t1:
if not t1[i].running:
t1[i] = Thread[(string,string,string,uint16,uint16)]()
createThread(t1[i], myFunction, (file, ... <other stuff>))
added_flag = true
break
if added_flag:
break
os.sleep(1)
joinThreads(t1)
for thr in t1:
assert not thr.running
I would expect all the threads in t1 to have finished running by the time I hit my assert statement, because I used joinThreads(), but that doesn't seem to be the case. Is my logic totally off? Am I misunderstanding how to properly use nim threads and joinThreads() in particular? Any help you could provide would be much appreciated. If your function is execProcess You can just use execProcesses instead to have multi-processing.
Your Thread[(string,string,string,uint16,uint16)] seems dangerous due to the strings not being GC-safe.
Thank you for the feedback! Unfortunately the function is not just an execProcess, it's just one step in the larger function.
Is there a work around for the GC-safe problem with passing string s? I need to pass the directory and file handles for the files I'm working with somehow...