2016-07-17 26 views
0

我有以下的代码,会异步触发回调为每个完成的任务一块被推任务如何在boost :: wait_for_any中考虑线程数量来正确调度任务?

#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等上的“战斗”。

我意识到这是一个不平凡的问题,它可能不止有一些解决方案/方法。

在线IDE http://melpon.org/wandbox/permlink/aME5cUecefZaBof4

回答

0

为了避免冲突,你可以做的第一件事就是不要开始/运行多个线程(同时)比有核(超线程)执行它们。

+0

这将是解决方案之一,但具体可以用这种方法做到这一点吗? – Patryk

相关问题