-
Notifications
You must be signed in to change notification settings - Fork 9
Open
Description
for example I have a simple program using std thread:
// sum_B_threads.cpp
#include <iostream>
#include <thread>
#include <vector>
#include <algorithm>
#include <cstdint>
int main() {
const int64_t N = 200000000LL; // 2e8
const unsigned T = 4;
std::vector<std::thread> threads;
std::vector<long long> partial(T, 0);
auto worker = [&](unsigned tid) {
const int64_t base = N / T;
const int64_t rem = N % T; // first 'rem' chunks get +1
const int64_t size = base + (tid < rem ? 1 : 0);
const int64_t start = tid * base + std::min<int64_t>(tid, rem) + 1;
const int64_t end = start + size - 1;
long long s = 0;
for (int64_t i = start; i <= end; ++i) s += i;
partial[tid] = s; // one slot per thread → no locking needed
};
threads.reserve(T);
for (unsigned t = 0; t < T; ++t) threads.emplace_back(worker, t);
for (auto &th : threads) th.join(); // wait for all threads to finish
long long total = 0;
for (auto s : partial) total += s;
std::cout << "Sum 1..200000000 (4 threads): " << total << "\n";
return 0;
}
and I get this error in the log in the attached file:
Reactions are currently unavailable
Metadata
Metadata
Assignees
Labels
No labels