英特尔 TBB 计算图:如何指定节点的输入队列容量



我正在寻找C++.NET TPL数据流库的模拟。

在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();

可以在官方文档中找到,有三种推荐的方法可以限制资源消耗,其中一种是使用limiter_node

限制资源消耗的一种方法是使用limiter_node来限制可以流经图形中给定点的消息

这不是你想要的确切东西,但仍然应该调查。我还能够找到并发队列类部分,这些部分可以通过set_capacity方法与有限容量一起使用。也许你可以这样管理它。希望这有帮助。

最新更新