我有以下代码,它会异步触发在任务向量上推送的每个已完成任务的回调
#define BOOST_THREAD_PROVIDES_FUTURE
#define BOOST_THREAD_PROVIDES_FUTURE_CONTINUATION
#include <fstream>
#include <iostream>
#include <random>
#include <thread>
#include <vector>
#include <boost/thread/future.hpp>
using vbf = std::vector<boost::future<std::pair<std::string, int>>>;
void processFile(vbf& v, const std::string& f, int offset)
{
v.emplace_back(boost::async([offset, &f]() {
std::cout << "Starting " << f << '\n';
std::ofstream ofs(f);
const int max = offset * 1000000;
for (int i = 0; i < max; ++i) ofs << i;
// line below wouldn't present the real problem
//std::this_thread::sleep_for(std::chrono::milliseconds(max));
return std::make_pair(f, max);
}));
}
void printProgress(vbf& tasks)
{
auto it = boost::wait_for_any(tasks.begin(), tasks.end());
while (it != tasks.end())
{
const auto res = it->get();
std::cout << "processed file \"" << res.first << "\" with " << res.second << " records\n";
tasks.erase(it);
it = boost::wait_for_any(tasks.begin(), tasks.end());
}
}
int main()
{
std::vector<std::string> vs{
"file1", "file2", "file3", "file4", "file5", "file6", "file7", "file8", "file9",
"file10", "file11", "file12", "file13", "file14", "file15", "file16", "file17", "file18",
"file19", "file20", "file21", "file22", "file23", "file24", "file25", "file26", "file27",
"file28", "file29", "file30", "file31", "file32", "file33", "file34", "file35", "file36",
"file37", "file38", "file39", "file40", "file41", "file42", "file43", "file44", "file45",
"file46", "file47", "file48", "file49", "file50"};
vbf v;
v.reserve(vs.size());
std::random_device rd;
std::mt19937 gen(rd());
std::uniform_int_distribution<> dis(1, 10);
for (const auto& f : vs) processFile(v, f, dis(gen));
printProgress(v);
}
通过写入来自std::vector<std::string> vs
的文件来“模拟”加载(有sleep()
的评论,但这不会出现真正的问题,因为CPU上没有实际负载)
我的问题是如何/通过什么方法可以正确地安排/排队在这里完成的工作,这样我就不会得到争用,线程在CPU上“打架”等等?
我意识到这是一个非常重要的问题,它可能只有少数解决方案/方法。
答案 0 :(得分:0)
为了避免争用,你要做的第一件事就是不要开始/运行更多线程(同时),而不是核心(超线程)来执行它们。