TBB管道库输入过滤器指导 [英] guidance with input filter for TBB Pipeline library

查看:139
本文介绍了TBB管道库输入过滤器指导的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我之前的问题中,我使用C ++(Linux)使用输入,变换和输出过滤器实现了TBB管道:

In my previous question, I implemented a TBB Pipeline using C++ (Linux) with Input, Transform and Output filters:

使用TBB管道输出不正确

incorrect output with TBB pipeline

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

The Input filter was reading data (C structure) from text files and was passing to Transform filter. The Transform filter was updating data and passing it to Output filter. The Output filter was storing it back on the disc. A simple read and write application.

现在,我想创建一个MAIN应用程序。 MAIN应用程序将在开始时创建具有三个过滤器的TBB管道:

Now, I am looking to create a MAIN application. The MAIN application will create a TBB Pipeline with three filters in the start:


  1. InputFilter:从主应用程序接收数据/

  2. OutputFilter:接收它并将信息返回到主应用程序。

  1. InputFilter : Receive data/C structure from the main application and pass it on.
  2. TransformFilter: Do some processing.
  3. OutputFilter : Receive it and return the information back to the main application.

开始时,InputFilter不会做任何数据/ C结构将为空。因此,它将循环或等待。

In the start, InputFilter will not do anything as data/C Structure will be empty. So it will be looping or waiting.

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

The MAIN application will read data from the file and will pass the information to the InputFilter (if required). The InputFilter will then process it and pass it to the next filter and so on. So the different is that :

输入由MAIN应用程序控制,而不是在InputFilter中(如我之前所述)。一种方法是通过引用InputFilter传递数据/ C结构,然后通过MAIN应用程序更新它。但问题是:

Input is controlled by the MAIN application not with in the InputFilter (as I did before). One way can be to pass data/ C structure by reference to the InputFilter and then update it via MAIN application. But problem is:

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


推荐答案

a href =http://www.threadingbuildingblocks.org/docs/help/reference/algorithms/pipeline_cls/thread_bound_filter_cls.htm =nofollow> thread_bound_filter documentation 页面,以使第一个过滤器作为线程-界。它工作正常,我认为这是你需要:

I've modified the example from thread_bound_filter documentation page in order to make the first filter as thread-bound. It works fine and I think it is what you need:

#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 code>通过 try_process_item()为了避免被阻止时,当令牌的数量已经超过..但一般的想法是一样的,你可以返回控制到处理过滤器的线程。

Of course, you may want to add more filters, change their type (except the first which must be serial), change number of tokens, use task::enqueue() instead of explicit thread, replace process_item() by try_process_item() in order to avoid being blocked inside when the number of tokens has exceeded.. but the general idea is the same, you can return the control to your thread which processes a filter.

这篇关于TBB管道库输入过滤器指导的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆