You're giving the thread function a pointer, &vector_locs[i]
, that may become invalidated as you push_back
more items into the vector.
Since you know beforehand how many items vector_locs
will contain - min(num_threads, array_size)
- you can reserve
that space in advance to prevent reallocation.
As to why it doesn't crash if you uncomment the output, I would guess that the output is so slow that the thread you just started will finish before the output is done, so the next iteration can't affect it.