2013-03-08 72 views
5

我一直在C++(作業調度程序庫?)中尋找一個可重用的執行管道庫。我在Boost內找不到任何東西。所以,我終於找到了兩位候選人:建立執行管道的C++庫

我失去了任何其他候選人?有沒有人使用過它們?它們在並行io和多線程方面有多好? 這些庫似乎仍然缺少依賴關係處理。比如,它似乎並不清楚,我怎麼一會寫東西,如:

$ cat /dev/urandom | tr P Q | head -3 

在這個非常簡單的情況下,管道走自下而上的,而當head過程停止拉動第一cat停止執行。

但是我不明白我怎麼可以從多線程和或並行IO的情況下受益,如:

$ cat /raid1/file1 /raid2/file2 | tr P Q > /tmp/file3 

有沒有辦法對我說:執行tr 7線程時8個處理器可用。

+0

我想你混淆了一句:管道並不自動意味着*各個步驟*在管道是paralleliseable(如你的例子說明),它只是意味着所有(或某些)步驟可以並行執行並在它們之間輪詢/推送數據。 – 2013-03-08 15:45:30

+1

我會進一步探索google併發性庫。它只是被提議成爲C++標準的一部分。更多在這裏http://www.open-std.org/JTC1/SC22/WG21/docs/papers/2013/n3534。html#解決方案 – 2013-04-07 12:55:29

回答

3

你在尋找的是一個數據流框架。管道是數據流的一種特殊形式,其中所有組件都有1個消費者和1個生產者。

加速支持數據流,但可惜的是,我並不熟悉的推動作用。這裏的鏈接:http://dancinghacker.com/code/dataflow/dataflow/introduction/dataflow.html

無論如何,你應該寫你的組件作爲單獨的程序,並使用UNIX管道。特別是,如果你的數據特徵是(或可以很容易地轉換成)行/文本。

也是一種選擇是寫自己的數據流的事情。這並不難,特別是當你有限制時(我指的是pipe:1-consumer/1-producer),你不應該實現一個完整的數據流框架。管道就是將某些功能綁定在一起,將其結果傳遞給下一個參數。數據流框架是關於組件接口/模式和綁定技術的。 (這很有趣,我已經寫了一個。)

+2

boost :: dataflow是一個尚未被接受進入boost的提案。 – rhashimoto 2013-04-03 15:51:47

2

我剛剛閱讀了關於RaftLib,它使用模板和類來創建管道元素,稱爲「內核」。除了並行數據流以外,它還允許使用像您所示的Bash示例的串行管道。從Hello world example頭版:

#include <raft> 
#include <raftio> 
#include <cstdlib> 
#include <string> 

class hi : public raft::kernel 
{ 
public: 
    hi() : raft::kernel() 
    { 
     output.addPort<std::string>("0"); 
    } 

    virtual raft::kstatus run() 
    { 
     output[ "0" ].push(std::string("Hello World\n")); 
     return(raft::stop); 
    } 
}; 


int 
main(int argc, char **argv) 
{ 
    /** instantiate print kernel **/ 
    raft::print<std::string> p; 
    /** instantiate hello world kernel **/ 
    hi hello; 
    /** make a map object **/ 
    raft::map m; 
    /** add kernels to map, both hello and p are executed concurrently **/ 
    m += hello >> p; 
    /** execute the map **/ 
    m.exe(); 
    return(EXIT_SUCCESS); 
}