Skip to content

Question: does LoopPoint work with other threading libraries like std thread/pthread besides OpenMP? #11

@koshostanford

Description

@koshostanford

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:

log.txt

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions