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();
看来,TBB流程图没有直接的接口来指定缓冲区的容量。但是,这可能是您的问题可以通过TBB以不同方式解决。为了权衡几个变体,你能提供更多关于你正试图解决的问题的信息吗? – Aleksei