小编Mic*_*hal的帖子

英特尔的多线程比AMD慢多了

我想使代码低于并行化:

for(int c=0; c<n; ++c) {
    Work(someArray, c);
}
Run Code Online (Sandbox Code Playgroud)

我这样做了:

#include <thread>
#include <vector>

auto iterationsPerCore = n/numCPU;
std::vector<std::future<void>> futures;

for(auto th = 0; th < numCPU; ++th) {
    for(auto n = th * iterationsPerCore; n < (th+1) * iterationsPerCore; ++n) {
        auto ftr = std::async( std::launch::deferred | std::launch::async,
            [n, iterationsPerCore, someArray]()
            {
                for(auto m = n; m < n + iterationsPerCore; ++m)
                    Work(someArray, m);
            }
        );
        futures.push_back(std::move(ftr));
    }

    for(auto& ftr : futures)
        ftr.wait();
}

// rest of iterations: n%iterationsPerCore …
Run Code Online (Sandbox Code Playgroud)

c++ concurrency multithreading c++11

8
推荐指数
2
解决办法
722
查看次数

标签 统计

c++ ×1

c++11 ×1

concurrency ×1

multithreading ×1