CAF:c++ actor framework
C++ actor framework簡單使用
簡介
下載/編譯/安裝
下載
編譯/安裝
簡單使用
單次調(diào)用,沒有使用線程池
復(fù)雜使用
思路
main.cpp 內(nèi)容如下
msgHandle.h
msgHandle.cpp
blocking_actor 類型說明
釋義
函數(shù)說明
其他loop 類型
其他說明
簡介
CAF 是 C++ Actor 模型框架,借鑒了 erlang 和 akka 的 actor 思想。有強 C++ 11 特性。 特點是:輕量級,分布式,簡單,可適應(yīng)以及無鎖。
官方文檔:https://actor-framework.readthedocs.io/en/latest/index.html
Github地址: https://github.com/actor-framework/actor-framework
wike地址: https://github.com/actor-framework/actor-framework/wiki
下載/編譯/安裝
下載
Linux – Git 下載方式
CAF GitHub 地址 : https://github.com/actor-framework/actor-framework
Git 下載 ,下載源碼 master :
備注:還需要安裝 boost 庫
git clone https://github.com/actor-framework/actor-framework.git
1
下載所有的 模塊和 庫。
git clone --recursive https://github.com/actor-framework/actor-framework.git
1
編譯/安裝
在linux 系統(tǒng)下載好后。在當前目錄下,能看到 actor-framework 文件夾。
執(zhí)行如下命令:
cd actor-framework #進入caf 文件夾 ./configure #執(zhí)行configure 腳本,配置對應(yīng)屬性,按照默認屬性配置 make #編譯 make install #安裝命令 make uninstall #卸載命令,如需卸載,執(zhí)行此命令就可以。
1
2
3
4
5
簡單使用
單次調(diào)用,沒有使用線程池
#include 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 編譯: g++ -g main.cpp -lcaf_core -o main 1 輸出如下: main.cpp string type msg is :String 類型消息 int type msg is:123456789 1 2 3 復(fù)雜使用 主要針對 : event_based_actor 類型說明 思路 啟用線程操作,根據(jù)項目的實際需求使用,定義不同的消息注釋,用來接收處理。 增加了如下文件:msgHandle.cpp msgHandle.h #include 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 · Atoms / 消息體智能指針 /behavior //定義actor 智能指針 msgHandleActor extern boost::shared_ptr 1 2 3 4 5 6 7 8 9 執(zhí)行類 - 詳細注釋不做說明。 class ActorRun{ public: ActorRun(); ~ActorRun(); static ActorRun* instance(); void startActorOne(std::string what); void startActorTwo(std::string what,int value); /** * @brief startActorDelay --延時發(fā)送處理函數(shù) * @param type --延時類型 0:毫秒 1:秒 * @param step --延時步長 * @param what --消息內(nèi)容 -- string類型值 * @param value --消息內(nèi)容 -- double類型值 */ void startActorDelay(bool type,int step,std::string what,double value); private: // 內(nèi)部計數(shù)值。循環(huán)次數(shù) int _count; }; 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 消息處理函數(shù) caf::behavior msgHandleFuntion(caf::event_based_actor *self) { return{ [=](actorOne it,std::string &what) { std::cout<<"actorOne: "< 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 atom 對應(yīng)消息發(fā)送函數(shù) void ActorRun::startActorOne(std::string what) { caf::anon_send(*msgHandelActor,actorOne::value,what); } void ActorRun::startActorTwo(std::string what,int value) { caf::anon_send(*msgHandelActor,actorTwo::value,what,value); } void ActorRun::startActorDelay(bool type,int step,std::string what,double value) { if(_count == 10) return; if(!type) caf::delayed_anon_send(*msgHandelActor,std::chrono::milliseconds(step),actorDelay::value,what,_count,value); else caf::delayed_anon_send(*msgHandelActor,std::chrono::seconds(step),actorDelay::value,what,_count,value); _count ++; } 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 編譯命令 g++ -g main.cpp msgHandle.cpp -lcaf_core -o main 1 運行結(jié)果 main.cpp actorOne: output:startOne actorTwo: output:startTwo value:123 actorDelay: count:0 text:output: value:0 actorDelay: count:1 text:output: value:1.11 actorDelay: count:2 text:output: value:2.22 actorDelay: count:3 text:output: value:3.33 actorDelay: count:4 text:output: value:4.44 actorDelay: count:5 text:output: value:5.55 actorDelay: count:6 text:output: value:6.66 actorDelay: count:7 text:output: value:7.77 actorDelay: count:8 text:output: value:8.88 actorDelay: count:9 text:output: value:9.99 1 2 3 4 5 6 7 8 9 10 11 12 13 blocking_actor 類型說明 Blocking actors always run in a separate thread and are not scheduled by CAF. Unlike event-based actors, blocking actors have explicit, blocking receive functions. Further, blocking actors do not handle system messages automatically via special-purpose callbacks (see Default and System Message Handlers). This gives users full control over the behavior of blocking actors. However, blocking actors still should follow conventions of the actor system. For example, actors should unconditionally terminate after receiving an exit_msg with reason exit_reason::kill。 具體翻譯不做說明,大概功能,類似與線程中,阻塞鎖,在生命周期內(nèi),一直處于活動狀態(tài),直到滿足特定條件,退出。 接收函數(shù) void blockingCalculatorFuntion(caf::blocking_actor *self) { bool running = true; self->receive_while(running) ( [](actorOne it,std::string &what) { std::cout<<"Block: -- "<<"actorOne: "< 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 消息指針定義,原子消息注釋定義 extern boost::shared_ptr 1 2 消息發(fā)送函數(shù) void ActorRun::startActorLoop_One() { caf::anon_send(*msgHandelLoopActor,actorOne::value,"Running"); } //退出函數(shù) void ActorRun::startActorLoop() { caf::anon_send(*msgHandelLoopActor,actorLoop::value,"Exit"); } 1 2 3 4 5 6 7 8 9 消息指針 spwan msgHandelLoopActor = boost::shared_ptr 1 消息發(fā)送-- main函數(shù)內(nèi)部 ActorRun::instance()->startActorLoop_One(); ActorRun::instance()->startActorLoop(); ActorRun::instance()->startActorLoop_One(); 1 2 3 執(zhí)行結(jié)果如下: main.cpp Block: -- actorOne: Running Block: -- Exit! 1 2 3 blocking_actor ,在退出之后,結(jié)束生命周期,處于disable狀態(tài),再次發(fā)送消息,不會響應(yīng)。在生產(chǎn)環(huán)境中使用,看個人理解了。 其他loop 類型 主要有三種循環(huán)接收,receive_while, receive_for and do_receive。很直觀,while,for,do-while 官方樣例如下: while size_t received = 0; receive_while([&] { return received < 10; }) ( [&](int) { ++received; } ); 1 2 3 4 5 6 for std::vector 1 2 3 4 5 6 7 do-while size_t received = 0; do_receive ( [&](int) { ++received; } ).until([&] { return received >= 10; }); 1 2 3 4 5 6 使用哪種循環(huán)方式,根據(jù)個人需求來使用了。 其他說明 一些特性說明: https://blog.csdn.net/xzwdev/article/details/41700001 還有一些其他功能.日志輸出,I/O功能。 一些相關(guān)特性:同步發(fā)送,異步發(fā)送,消息跳過沒有做相關(guān)說明,可以參考官方文檔。 C++
版權(quán)聲明:本文內(nèi)容由網(wǎng)絡(luò)用戶投稿,版權(quán)歸原作者所有,本站不擁有其著作權(quán),亦不承擔相應(yīng)法律責任。如果您發(fā)現(xiàn)本站中有涉嫌抄襲或描述失實的內(nèi)容,請聯(lián)系我們jiasou666@gmail.com 處理,核實后本網(wǎng)站將在24小時內(nèi)刪除侵權(quán)內(nèi)容。
版權(quán)聲明:本文內(nèi)容由網(wǎng)絡(luò)用戶投稿,版權(quán)歸原作者所有,本站不擁有其著作權(quán),亦不承擔相應(yīng)法律責任。如果您發(fā)現(xiàn)本站中有涉嫌抄襲或描述失實的內(nèi)容,請聯(lián)系我們jiasou666@gmail.com 處理,核實后本網(wǎng)站將在24小時內(nèi)刪除侵權(quán)內(nèi)容。