繁体   English   中英

带TBB管道库输入过滤器的指南

[英]guidance with input filter for TBB Pipeline library

在上一个问题中,我使用带有输入,转换和输出过滤器的C ++(Linux)实现了TBB管道:

TBB管道输出不正确

输入过滤器正在从文本文件读取数据(C结构),并将其传递给变换过滤器。 转换过滤器正在更新数据,并将其传递给输出过滤器。 输出过滤器将其重新存储在光盘上。 一个简单的读写应用程序。

现在,我希望创建一个MAIN应用程序。 MAIN应用程序将首先创建带有三个过滤器的TBB管道:

  1. InputFilter:从主应用程序接收数据/ C结构并将其传递。
  2. TransformFilter:做一些处理。
  3. OutputFilter:接收它,然后将信息返回给主应用程序。

    首先,InputFilter将不执行任何操作,因为data / C Structure将为空。 因此它将循环或等待。

    MAIN应用程序将从文件中读取数据,并将信息传递给InputFilter(如果需要)。 然后,InputFilter将对其进行处理,并将其传递给下一个过滤器,依此类推。 所以不同的是:

    输入由MAIN应用程序控制,而不是由InputFilter来控制(就像我之前所做的那样)。 一种方法是通过引用InputFilter传递数据/ C结构,然后通过MAIN应用程序对其进行更新。 但是问题是:

    控件永远不会从InputFilter返回到MAIN应用程序。 任何帮助将不胜感激!

我修改了thread_bound_filter文档页面中的示例,以使第一个过滤器成为线程绑定的。 它工作正常,我认为这是您需要的:

#include <iostream>
#include "tbb/compat/thread"
#include "tbb/pipeline.h"

using namespace tbb;
using namespace std;

char InputString[] = "abcdefg\n";

class InputFilter: public thread_bound_filter {
    char* my_ptr;
public:
    void* operator()(void*) {
        if (*my_ptr)
            return my_ptr++;
        else
            return NULL;
    }
    InputFilter()
    : thread_bound_filter( serial_in_order ), my_ptr(InputString)
    {}
};

class OutputFilter: public filter {
public:
    void* operator()(void* item) {
        std::cout << *(char*)item;
        return NULL;
    }
    OutputFilter() : filter(serial_in_order) {}
};

void RunPipeline(pipeline* p) {
    p->run(8);
}

int main() {
    // Construct the pipeline
    InputFilter f;
    OutputFilter g;
    pipeline p;
    p.add_filter(f);
    p.add_filter(g);

    // Another thread initiates execution of the pipeline
    thread t(RunPipeline, &p);

    // Process the thread_bound_filter with the current thread.
    while (f.process_item()!=thread_bound_filter::end_of_stream)
        continue;

    // Wait for pipeline to finish on the other thread.
    t.join();

    return 0;
}

当然,您可能想要添加更多过滤器,更改其类型(除了第一个必须是串行的),更改令牌数量,使用task::enqueue()代替显式线程,将process_item()替换为try_process_item()为了避免在令牌数量已超过时被阻塞在内部..但总体思路是相同的,您可以将控件返回到处理过滤器的线程。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM