【发布时间】:2020-07-17 19:21:36
【问题描述】:
尝试并行化计算方法,然后等待从所有计算中收集结果并将它们与其他数据注入一起存储在 std::list 中,因此看不到性能提升。也许如果我知道'num_results',我可以通过锁定(互斥)写入(push_back)过程以某种方式合并两个周期并将数据存储在列表中?另一件事是如何保持顺序(如果在某些 [i] 上计算速度很快) 在一个内核的一个周期中这样做太慢了。
// here is pseudocode
void SomeClass::SomeMethod() {
size_t num_results = request_list.size();
std::list<result_t> some_results;
float *result = new float[num_results];
// linearly I do one for in where one parameter of list pushing are long computing function
// so I create array of function results and try to store data same time then wait and collect in list with other data
for (size_t i(0); i < num_results; i++) {
// Calculate is hard function and may vary in times depending on imput
// use temporary thread object and labda function to acces class members data
thread t([&]() { result[i] = Calculate(request_list[i]); });
// where or how to wait for all results stored in array only then push them to list?
t.join(); // where or how to wait for all result[] for next cycle or merge both?
}
// conjugate result with some other data from static list with same id's
for (std::size_t i(0); i < num_requests; i++) {
some_results.push_back( result_t(result[i], other_data[i], ...) );
}
delete [] result; // free memory
// Continue job with some_results list
}
我的并行操作是不是错了?
【问题讨论】:
标签: c++ multithreading asynchronous std