c++中类似python的多处理
Python-like multiprocessing in C++
我是一个c++新手,我有很长的Python背景。
我正在寻找一种在c++中并行运行函数的方法。我读了很多关于std::async
的书,但对我来说仍然不是很清楚。
-
下面的代码做了一些非常有趣的事情
#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
之后的方法。 -
关于并行运行。如果我有这样的代码:
#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
相关文章:
- 将数据从 c++ 文件连续发送到 Python 脚本以进行进一步处理
- 使用python预处理后,C++(opencv)中的垫子类型数据与image_to_array相同
- 如何在 Python 3 中正确处理用于嵌入的字符串
- 如果C/C Python3扩展托管HTTP服务器并将Python代码称为请求处理程序,则如何处理并发
- Boost.python 中的error_already_set做什么以及如何在 Python C API 中类似地处理
- 如何在 Python 中的 SWIG Director 方法中处理空指针
- 使用CTYPE将数组从Python传递到C ,无法完全处理
- 从 exe 获取输出C++并在 Python 中实时处理它
- 重命名批处理文件(删除名称中的一些符号)(python/matlab/shell)
- 如何处理python导入模块中c++断言导致的Celery WorkerLostError
- 无法让 matplotlib 事件处理程序与 Boost.Python 一起工作
- boost.python向现有的PyObject添加绑定(对于例外处理)
- 我如何有boost.python调用一个函数来处理对象
- 包装使用OpenCV进行图像处理的C代码,以便与同样使用OpenCV的Python一起使用
- 如何在Python中处理C++中的PyObject*
- DICOM图像处理,可视化和GUI-C++和Python
- 将异常处理从 python 转换为 C++
- 处理 char * 的正确方法在 swig/python 中返回数据
- Python 和 C++ 中的配置文件处理
- 如何在boost::Python中处理Python切片对象