Hi, hope everyone has good time with Zig like myself.
I would like to ask a very basic question regarding threading.
The short code below just works fine for my study purpose, but I would like to wait before it prints “Bye” in an idiomatic way - not calling the join twice as well as without the defer.
I don’t see anything wrong or “non-idiomatic” with your code.
Windows has WaitForMultipleObjects which allows joining for various threads at once, or wait for at least one to complete, but I don’t think Zig’s std exposes that.
That errdefer doesn’t make sense, it needs to be right after assigning to threads so that the zero indexed thread gets joined in case the one indexed attempt to spawn a thread fails.
If it is below the assign to the one indexed thread you already know that the spawn was successful and the for loop takes care of both. The errdefer is so that you don’t forget joining the zero indexed one.
Don’t just reorder statements because it “looks cleaner”.
Also that whole block should be in its own scope so that the errdefer stops being active when the threads slice was successfully initialized. (you don’t want const thread2 = try ... to fail an trigger the errdefer way above because that would call join again.)
Also with thread2 and thread3 you aren’t handling error at all, just exiting the program without joining any potentially still fine threads.
Basically you aren’t handling failure well, you should study the nuance of the code in @dimdin’s and @dude_the_builder’s answers they are carefully written to handle failure at any point.
If you are unsure about error handling, force the error in different spots and see what happens, for example by just calling a failing function instead:
Thanks for correcting my mistake as well as detailed advises.
Yes, I blindly copied the answer (and happy it is running!), but you are right - I should understand what is the meaning for each line the order, especially in this low level programming environment.
Hope I can have better eyes when I bring another question.