c++ - Thread Building Blocks 流程图 — 类似 limiter_node 的东西,不会丢弃消息

标签 c++ multithreading tbb

是否有可能有类似 limiter_node<T> 的东西?它可以防止队列溢出,但不是丢弃不适合队列的消息,而是将它们转发到其他节点?我想要类似的东西

typedef std::string S;
using namespace tbb::flow;
source_node<S> input(g, Input(), false);
limiter_node<S> limiter(g, queue_len);
function_node<S, S> processor(g, threadpool_size, Processor());
function_node<S, int> output(g, serial, Output());

make_edge(input, limiter);
make_edge(limiter, processor);
make_edge(limiter.magic_forwarder_of_failed_deliveries_thank_you, output); // ???
make_edge(processor, output);
make_edge(output, limiter.decrement);

基本上,我想用大量阻塞 IO 绑定(bind)的“节点”处理尽可能多的数据,并将其余数据直接转发到未处理的输出节点。

最佳答案

(披露:我在英特尔从事英特尔线程构建模块的工作。)

我同意 yohjp 的观点,你可以用 multifunction_node 来做到这一点。

typedef multifunction_node< S, tuple< S, S > > mnode_t;

struct Forwarder {
// This assumes
//   port 0 is connected to the limiter
//   port 1 is the alternative path
void operator()( const S &s, mnode_t::output_ports_type &outs ) {
    if ( !get<0>(outs).try_put(s) ) {
        get<1>(outs).try_put(s);
    }
} };

随着边缘的改变,使得:

make_edge(input, forwarder);
make_edge(output_port<0>(forwarder), limiter);
make_edge(limiter, processor);
make_edge(output_port<1>(forwarder), output);

您还必须想出一种方法,使减量只对那些通过处理器的事情进行调用。

关于c++ - Thread Building Blocks 流程图 — 类似 limiter_node 的东西,不会丢弃消息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16854354/

相关文章:

c++ - 如何将这些串口的参数传递给函数

c++ - 如何强制github识别以特定语言编写的文件?

Linux 性能 : is it possible to somehow ignore busy waiting threads?

c++ - for 循环 : poor efficiency in my code 的 OpenMP 并行化

java - 多线程套接字服务器通过消息关闭Java

c++ - 如何使用 TBB 并行化 std::partition

c++ - Boost Graph - 同时使用有向边和无向边

c++ - 从文件名中获取目录名

c++ - PPL when_all 具有不同类型的任务?

multithreading - TBB 在 Matlab Mex 文件中表现奇怪