c++中类似python的多处理

Python-like multiprocessing in C++

本文关键字:处理 python c++      更新时间:2023-10-16

我是一个c++新手,我有很长的Python背景。

我正在寻找一种在c++中并行运行函数的方法。我读了很多关于std::async的书,但对我来说仍然不是很清楚。

  1. 下面的代码做了一些非常有趣的事情

    #include <future>
    #include <iostream>
    void called_from_async() {
      std::cout << "Async call" << std::endl;
    }
    int main() {
      //called_from_async launched in a separate thread if possible
      std::future<void> result( std::async(called_from_async));
      std::cout << "Message from main." << std::endl;
      //ensure that called_from_async is launched synchronously
      //if it wasn't already launched
      result.get();
      return 0;
    }
    

    如果我运行它几次,有时输出是我所期望的:

    Message from main.
    Async call
    

    但有时我得到这样的东西:

    MAessysnacg ec aflrlom main.
    

    为什么不先发生cout ?我明确地将.get()方法称为cout之后的方法。

  2. 关于并行运行。如果我有这样的代码:

    #include <future>
    #include <iostream>
    #include <vector>
    int twice(int m) {
      return 2 * m;
    }
    int main() {
      std::vector<std::future<int>> futures;
      for(int i = 0; i < 10; ++i) {
        futures.push_back (std::async(twice, i));
      }
      //retrive and print the value stored in the future
      for(auto &e : futures) {
        std::cout << e.get() << std::endl;
      }
      return 0;
    }
    

    twice函数的所有10个调用将同时在单独的核心上运行?

    如果不是,c++中是否有类似的东西,如Python multiprocess lib?

    主要是我在找什么:

    我写了一个函数,用n个输入调用它,用multiprocessing?并且它将在n个节点上同时运行该函数1次

1) result.get();未启动线程。它只在等待结果。调用std::async(called_from_async)(或编译器决定的任何时间)启动并行线程。

然而,std::cout保证是内部线程安全的。因此,您向我们显示的结果不应该出现。这里有一个竞争条件,但是你不能像那样混合两个输出。如果它真的发生了(我怀疑),那么你可能是在处理一个编译器错误。

2)您的呼叫将并行运行。有多少核取决于操作系统和机器上运行的其他进程。但有一个很好的机会,所有将被使用(假设你控制了整个生态系统,没有其他cpu密集型进程在后台运行)。

c++没有类似多处理的库(至少在标准中没有)。如果您希望运行子进程,那么有几个选项,例如fork或popen系统调用。

我在异步调用的帮助下通过计算矩阵扩展了上述解决方案。这为我在计算气候数据相关矩阵时节省了很多时间。(只是以防别人需要这个来计算一些东西)

#include <future>
#include <iostream>
#include <vector>
int twice(int m) {
    return 2 * m;
}
void compute_row(int* row) {
    for (unsigned i = 0; i < 10; i++) row[i] = twice(i);
}
void compute(int** matrix) {
    std::vector<std::future<void> > futures;
    for (int i = 0; i < 10; ++i) futures.push_back(std::async(compute_row, matrix[i]));
    // wait for it
    for (auto& e : futures) e.get();
}
int main() {
    int** results = new int*[10];
    for (unsigned i = 0; i < 10; i++) results[i] = new int[10];
    compute(results);
    for (unsigned i = 0; i < 10; i++) {
        for (unsigned j = 0; j < 10; j++) std::cout << results[i][j] << " ";
        std::cout << std::endl;
    }
    return 0;
}

编译g++ std=c++11 -o doit doit.cpp