2017-07-18 128 views
0

我正在尋找.NET TPL Dataflow庫的C++模擬。英特爾TBB計算圖:如何指定節點的輸入隊列容量

在TPL數據流中,您可以指定塊的容量選項的並行度&。如果該塊的輸入隊列的大小達到它的容量,則相應的塊的生成器的執行被掛起:

var buffer = new BufferBlock<int>(new DataflowBlockOptions() { BoundedCapacity = 10 }); 

var producer = new Task(() => { 
    for (int i = 0; i < 1000; i++) { 
     buffer.Post(i); 
    } 
}); 

var fstAction = new TransformBlock<int, int>(async (i) => { 
    return i*i; 
}, MaxDegreeOfParallelism = 4, BoundedCapacity = 10); 

var sndAction = new ActionBlock<int>(async (i) => { 
    Thread.Sleep(5000); 
    Console.WriteLine(i); 
}, MaxDegreeOfParallelism = 4, BoundedCapacity = 10); 

buffer.LinkTo(fstAction, new DataflowLinkOptions() { PropagateCompletion = true }); 
fstAction.LinkTo(sndAction, new DataflowLinkOptions() { PropagateCompletion = true }); 

sndAction.Completition.Wait(); 

我需要用C類似的功能++。 TBB似乎是一個很好的選擇,但是我找不到如何在function_node/buffer_node上指定容量。下面是一個例子:

std::size_t exportConcurrency = 16; 
std::size_t uploadConcurrency = 16; 

flow::graph graph; 

std::size_t count = 1000; 
std::size_t idx = 0; 

flow::source_node<std::vector<std::string>> producerNode(graph, [&count, &idx](auto& out) { 
    out = { "0"s }; 
    return ++idx != count; 
}); 

flow::function_node<std::vector<std::string>, std::string> exportNode(graph, exportConcurrency, [](auto& ids) { 
    return "0"s; 
}); 

flow::function_node<std::string, std::string> uploadNode(graph, uploadConcurrency, [](auto& chunk) { 
    std::this_thread::sleep_for(5s); 
    return "0"s; 
}); 

flow::make_edge(producerNode, exportNode); 
flow::make_edge(exportNode, uploadNode); 

graph.wait_for_all(); 
+0

看來,TBB流程圖沒有直接的接口來指定緩衝區的容量。但是,這可能是您的問題可以通過TBB以不同方式解決。爲了權衡幾個變體,你能提供更多關於你正試圖解決的問題的信息嗎? – Aleksei

回答

0

人們可以在official docs發現,有三種推薦方式來limiting resource consumption,其中之一是using limiter_node

一種方式來限制資源消耗是使用limiter_node來設置數量的消息的限制,該限制可以通過圖形中的給定點。

它不是你想要的確切的東西,但仍然應該調查。此外,我能找到concurrent queue classes部分,可以使用有限容量與set_capacity方法。也許你可以這樣管理它。希望這可以幫助。