C++11 使用std::thread组合来自多个线程的数组 我在C++中编写了一个蒙特卡罗程序,AM使用STD::线程来划分要在线程之间跟踪的历史数。然而,这是我第一次尝试多线程,我遇到了一个问题,希望这个简化的代码能让我演示一下,希望我能从这个网站的读者那里得到一些建议。在这个简化的问题中,我调用函数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; } #包括 #包括 #包括 #包括 #包括 #包括“boost/multi_array.hpp” std::矢量阵列; std::mutex数组\u mutex; 无效总和(整数样本大小) { std::锁定保护(数组互斥); 对于(int i=0;i

C++11 使用std::thread组合来自多个线程的数组 我在C++中编写了一个蒙特卡罗程序,AM使用STD::线程来划分要在线程之间跟踪的历史数。然而,这是我第一次尝试多线程,我遇到了一个问题,希望这个简化的代码能让我演示一下,希望我能从这个网站的读者那里得到一些建议。在这个简化的问题中,我调用函数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; } #包括 #包括 #包括 #包括 #包括 #包括“boost/multi_array.hpp” std::矢量阵列; std::mutex数组\u mutex; 无效总和(整数样本大小) { std::锁定保护(数组互斥); 对于(int i=0;i,c++11,stdthread,C++11,Stdthread,对于一个简单而幼稚的解决方案 #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_devi

对于一个简单而幼稚的解决方案

#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';
}
#包括
#包括
#包括
#包括
void生成(常量大小、标准::数组和值)
{
//伪随机数生成
std::随机_装置rd;
std::默认的随机引擎e1(rd());
标准:均匀分布均匀分布(1,10);
//生成一些值并添加数组
对于(大小i=0;istd::cout两件事:让每个线程执行一点计算,这样一个线程执行条目1到10,另一个线程执行条目11到20,依此类推那么你就不需要锁了。另一件事是,只初始化随机数生成器一次。而且,你没有多个数组(或向量)可以“组合”在您的代码中,所有线程都将写入同一个数组。事实上,忽略我前面评论的第一部分,因为您的问题是另一个问题,即您没有多个向量,只有一个。好的,这是一个很好的信息,它改变了我对输出的理解。如果是同一个数组,那么这意味着来自一个线程的数据正在覆盖第二个线程生成的数据,这就是为什么我用std::cout命令打印它们时会得到两个数组,但实际上是同一个数组/内存插槽用被覆盖的数据打印了两次。如果这是真的,那么我如何让每个线程生成一个单独的数组,可以组合到另一个线程或主程序中
std::rand
之所以糟糕,有很多原因是它缺乏线程安全性。你应该改用。我在这个例子中只使用了std::rand,通常我使用的是为MCNP代码开发的LANL RNG,但我不想让任何人对它在这个网站上的实现感到困惑。谢谢你的例子,一张图片描绘了一个想法usand WORD!;-)除了一些问题外,代码运行得非常好。唯一的问题是必须在函数调用中分配值数组。不幸的是,我的问题需要动态数组分配,因为我永远不知道在用户定义的问题中将使用多少蒙特卡罗检测器。我尽了我所能想想向量和点,什么都不起作用。最后,我以一种公正的方式分配了数组,以确保用户可以定义任意多的检测器。感谢对本示例的帮助。