使用std :: thread组合来自多个线程的数组

时间:2015-02-09 18:35:06

标签: c++11 stdthread

我正在用C ++编写蒙特卡洛程序,并使用std :: thread来划分线程之间要跟踪的历史数量。然而,这是我第一次尝试多线程,我遇到了一个问题,这个简化的代码应该让我展示希望我可以从本网站的读者那里得到一些建议。在这个简化的问题中,我调用的函数Summation生成一个带有2个线程的随机数的1X5维数组。当线程返回它们的值(因为它是一个全局变量而没有真正返回)时,主程序就有两个五维数组,每个数组对应一个不同的线程。我想将两个数组合并为一个数组,最后一个数组中的元素对应于由不同线程生成的两个数组中相同元素的总和。不幸的是,每个线程的数组都有相同的名称,所以我不能简单地将两个不同的数组一起添加。建议使用哪些方法将两个1X5维数组合成一个求和数组,其中元素对应于每个线程中相同元素的总和?

#include <iostream>
#include <vector>
#include <thread>
#include <mutex>
#include <cassert>
#include "boost/multi_array.hpp"

std::vector<float> Array;
std::mutex Array_mutex;

void Summation(int sample_size)
{
    std::lock_guard<std::mutex> guard(Array_mutex);
    for(int i = 0; i < sample_size; i++)
    {
        Array.push_back(rand() % 10 + 1);
    }
    std::cout << "\n";
}

int main(int argc, const char * argv[]) {
    int sample_size = 10;
    int Num_Threads = 2;
    int number_count = sample_size/Num_Threads;
    srand(time(NULL));
    std::vector<std::thread> Threads;
    for(int i = 0; i < Num_Threads; i++)
    {
        Threads.push_back(std::thread(Summation,number_count));
    }

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

    // - I would like to combine the arrays produced from each thread into a
    //   single array, where each element in the final array is the sum of
    //   the identical element in the array from each thread

    // i.e. Element 1(final) = Element 1(thread 1) + Element 1(thread2)
    //      Element 2(final) = Element 2(thread 1) + Element 2(thread2)
    //      Element 3(final) = Element 3(thread 1) + Element 3(thread2)

    return 0;
}

1 个答案:

答案 0 :(得分:1)

如果每个线程需要一个向量,实际上每个线程需要一个向量。像矢量矢量。

对于简单而天真的解决方案,例如

#include <iostream>
#include <array>
#include <random>
#include <thread>

void generate(const size_t size, std::array<float>& values)
{
    // Pseudo-random number generation stuff
    std::random_device rd;
    std::default_random_engine e1(rd());
    std::uniform_int_distribution<float> uniform_dist(1, 10);

    // Generate some values and add the array
    for (size_t i = 0; i < size; ++i)
        values[i] = uniform_dist(el);
}

int main()
{
    constexpr size_t number_values  = 10;
    constexpr size_t number_threads = 2;

    // An array of arrays, one sub-array per thread
    std::array<std::array<float, number_values>, number_threads>
        values;

    // An array of threads
    std::array<std::thread, number_threads> threads;

    // Create threads
    for (size_t i = 0; i < number_threads; ++i)
        threads[i] = std::thread(generate, number_values, std::ref(values[i]));

    // Wait for threads to finish
    for (size_t i = 0; i < number_threads; ++i)
        threads[i].join();

    // Now "combine" the values into a single array
    std::array<float, number_values> totals;
    for (size_t i = 0; i < number_values; ++i)
    {
        for (size_t j = 0; j < number_threads; ++j)
            totals[i] += values[j][i];
    }

    // Print the values
    for (const size_t i; i < number_values; ++i)
        std::cout << "Value #" << (i + 1) << " = " << totals[i] << '\n';
}

请注意,代码未经测试,甚至没有编译,但理论上应该 。 :)