首页 > 解决方案 > 使用 std::async 同时更新子数组

问题描述

我正在尝试通过将其拆分为较小的数组来同时更新游戏循环中的数组

//sample code but 'arr' can be of any type
const int size = 1000000;
double arr[size];
while(running)
{
    double dt = delta();
    auto a = std::async([&](double tdt)
     {
         for(int i = 0; i < size / 2; i++)
             arr[i] *= tdt;
     }, dt);
    auto b = std::async([&](double tdt)
     {
         for(int i = size / 2; i < size; i++)
             arr[i] *= tdt;
     }, dt);

     /*do something with arr after*/

}

但我不确定我在这里做什么。

标签: c++multithreadingc++17

解决方案


推荐阅读