std::thread t(f, args...) starts running f on a new thread straight away. t.join() waits until it has finished; until then, nothing it computed is safe to read. A std::thread that is destroyed while it could still be joined calls std::terminate and ends the program, so every thread is joined (or, rarely, detached) before its object goes away.
Arguments are copied into the new thread. To pass a reference, wrap it in std::ref, or start the thread with a lambda that captures what it needs. Capture carefully: a lambda that captures a loop variable by reference sees whatever value it has when the thread finally runs.
The simplest safe design gives each thread its own output (its own variable, or its own slot in a vector) and combines the results after join.
Threads here are simulated. Your program runs on one real thread while a scheduler switches between your threads at shared reads and writes, locks, atomic operations, waits, yields and sleeps, and every test runs under 12 different schedules. The order things can happen in, races at those points and deadlocks are faithful. Real parallel speed-up and weak memory-order effects are not.
Each thread writes its own result; join before reading it.