Use fixed result slots when worker threads produce values independently.

Thread Result Slots

thread_result_slots.cpp
#include <iostream>
#include <thread>
#include <vector>

void fillSlot(int slot, int* results) {
    int value = (slot + 1) * 10;
    results[slot] = value;
}

int main() {
    int workerCount = ;
    int results[3] = {0, 0, 0};
    std::vector<std::thread> workers;

    for (int i = 0; i < workerCount; ++i) {
        workers.emplace_back(fillSlot, i, results);
    }

    for (int i = 0; i < workerCount; ++i) {
        workers[i].join();
    }

    int total = 0;
    for (int i = 0; i < workerCount; ++i) {
        total += results[i];
    }

    std::cout << "workers=" << workerCount << std::endl;
    std::cout << "total=" << total << std::endl;
    return 0;
}
#include <iostream>
#include <thread>
#include <vector>

void fillSlot(int slot, int* results) {
    int value = (slot + 1) * 10;
    results[slot] = value;
}

int main() {
    int workerCount = ;
    int results[3] = {0, 0, 0};
    std::vector<std::thread> workers;

    for (int i = 0; i < workerCount; ++i) {
        workers.emplace_back(fillSlot, i, results);
    }

    for (int i = 0; i < workerCount; ++i) {
        workers[i].join();
    }

    int total = 0;
    for (int i = 0; i < workerCount; ++i) {
        total += results[i];
    }

    std::cout << "workers=" << workerCount << std::endl;
    std::cout << "total=" << total << std::endl;
    return 0;
}
#include <iostream>
#include <thread>
#include <vector>

void fillSlot(int slot, int* results) {
    int value = (slot + 1) * 10;
    results[slot] = value;
}

int main() {
    int workerCount = ;
    int results[3] = {0, 0, 0};
    std::vector<std::thread> workers;

    for (int i = 0; i < workerCount; ++i) {
        workers.emplace_back(fillSlot, i, results);
    }

    for (int i = 0; i < workerCount; ++i) {
        workers[i].join();
    }

    int total = 0;
    for (int i = 0; i < workerCount; ++i) {
        total += results[i];
    }

    std::cout << "workers=" << workerCount << std::endl;
    std::cout << "total=" << total << std::endl;
    return 0;
}
slots Each thread writes to a different vector slot, so the final join can summarize the results without shared mutation on one counter.
replay The replay panel can switch between thread IDs as the worker events interleave.