You should not expect any ordering between threads.
If you start a new thread, it is simply added to the operating system management structures. In the end, the thread scheduler appears and allocates a time slice for the thread. He can do this in a circular way, choose a random one, use some heuristics to determine which one is most important (for example, the one that owns the window in the foreground), etc.
If the order of the exits is relevant, you can either sort it later, or - if you know that the order has already begun before starting work - use an array in which each thread is assigned an index into which it must write its result.
Creating new threads as your example does is also very slow. For a microtask, using a thread pool is at least one order of magnitude higher.
Cygon source share