65.9K
CodeProject 正在变化。 阅读更多。
Home

AxPipe - 多线程二进制流 C++ 类库

starIconstarIconstarIcon
emptyStarIcon
starIcon
emptyStarIcon

3.71/5 (7投票s)

2004年1月12日

2分钟阅读

viewsIcon

62754

downloadIcon

1121

一组小巧高效的类和模板,用于创建多阶段多线程数据处理管道

引言

AxPipe 是一个非常小巧高效的类库,实现了类似于 Unix Shell 管道的扩展流和管道抽象。 它的突出特点是:推送和拉取模型处理,运行时构建的多级管道,可选的按阶段应用的多线程,拆分,合并以及非常小的占用空间。 它具有完整的文档,包括示例代码和一些现成的转换。

当前的实现是针对带有 Visual Studio 6 或 2002 的 Win32 API,但是移植到 Unix 应该没有什么困难,只需要修改线程和协同程序支持。 AxPipe 是一个易于学习和应用的轻量级库。 目前,它正被用于开发 AxCrypt 的下一个版本,http://axcrypt.sourceforge.net

它仍在积极开发中,我感谢任何和所有建设性的反馈和改进建议,或者迁移到现有框架或库的理由,这些框架或库可以做得更好/更快/更小/更简洁/更安全/任何东西。

使用代码

源代码带有完整的演示应用程序和完整的文档,涵盖每个命名空间、类、成员、方法、枚举、定义、模板等。

所有内容也可以在项目主页 http://axpipe.sourceforge.net 上找到,更新的版本也会在那里找到。 您将在那里找到代码示例和进一步的概述。

引言

基本范例取自 Unix Shell 管道符号,您可以在其中编写

crypt <file.txt | compress | tar >out.tar
     but you can also write, for example,
tar <file.txt | crypt | compress >out.z

上面的程序是半虚构的,只是为了演示输入源(例如文件)如何重定向到处理程序中,处理程序将其发送出去,在那里它可以连接到另一个处理程序,或连接到最终目的地。

我经常想在 C++ 中使用相同的原则进行编程,但要以最小的开销并支持不同的编程模型。 所以我编写了这个包。

以下是一个最小的完整程序,演示了基本用法。

/*! \file
    \brief A most simple AxPipe program

    Read an input file, pass it through a do-nothing
    pipe, and write it to a file. No error checking
    at all for clarity.
*/

#include "stdafx.h"
#include "AxPipe.h"
#include "CFileMap.h"

/// \brief Just pass data along - do nothing with it.
class CPipeDoNothing : public AxPipe::CPipe {
    void Out(AxPipe::CSeg *pSeg) {
        // Insert your code here...
        Pump(pSeg);
    }
};

int
_tmain(int argc, _TCHAR* argv[]) {
    AxPipe::CGlobalInit axpipeInit;    // It just has to be there.

    AxPipe::CSourceMemFile sourceFile; // The source is a memory mapped file

    sourceFile.Init(argv[1]);          // Set the source file name
    // Append a stage, in it's own thread
    sourceFile.Append(new AxPipe::CThread<CPipeDoNothing>);
    // Continue to append a sink to accept the output
    sourceFile.Append((new AxPipe::CSinkMemFile)->Init(argv[2]));

    // Initialize, Process the data, End process and Finalize.
    sourceFile.Open()->Drain()->Close()->Plug();

    // Check for any errors in any parts...
    if (sourceFile.GetErrorCode()) {
        // Print a clear text representation of the problem
        fprintf(stderr, sourceFile.GetErrorMsg());
        return sourceFile.GetErrorCode();
    }

    return 0;
}

关注点

如果您从未见过协同程序(Win32 API 中的 CreateFiber() 等)的用途,那么这是一个非常好的用途,它将推送模型处理过滤器转换为拉取模型过滤器。 与大多数类似的包相反,我相信 AxPipe 允许您选择性地并且在运行时构建具有不同阶段的、并且在阶段基础上具有或不具有线程的处理管道。

它对其外部依赖项的使用非常严格,并且即使在最小的运行时支持下也适用于非常小的应用程序。 因此它不使用异常,当然也不使用 MFC 或类似的大型框架。 还有一些“库存”转换,是根据我在使用 AxPipe 的项目中需要的用例开发的,但长远来看,目标是收集大量常见来源、转换和接收器,并将它们放入一个非常易于重用的现成组件库中。

© . All rights reserved.