CAF:c++ actor framework

      網(wǎng)友投稿 1043 2025-03-31

      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 #include #include #include caf::behavior msgHandle(caf::event_based_actor *self) { return{ [=](std::string &what) { std::cout<<"string type msg is :"<

      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 #include #include #include #include #include "msgHandle.h" int main() { std::cout<<"main.cpp"<(new caf::actor(self->spawn(msgHandleFuntion))); ActorRun::instance()->startActorOne("output:startOne"); ActorRun::instance()->startActorTwo("output:startTwo ",123); ActorRun::instance()->startActorDelay(false,5,"output: ",0); self->await_all_other_actors_done(); system.await_all_actors_done(); return 0; }

      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 msgHandelActor; //定義原子-消息注釋 actorOne,actorTwo,actorDelay // 注意消息內(nèi)容長度小于11 using actorOne = caf::atom_constant; using actorTwo = caf::atom_constant; using actorDelay = caf::atom_constant; //函數(shù)聲明 msgHandleFuntion caf::behavior msgHandleFuntion(caf::event_based_actor *self);

      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

      CAF:c++ actor framework

      17

      18

      19

      消息處理函數(shù)

      caf::behavior msgHandleFuntion(caf::event_based_actor *self) { return{ [=](actorOne it,std::string &what) { std::cout<<"actorOne: "<startActorDelay(true,5,"output: ",1.11*(count+1)); } }; }

      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 msgHandelLoopActor; using actorLoop = caf::atom_constant;

      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(new caf::actor(self->spawn(blockingCalculatorFuntion)));

      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 results; size_t i = 0; receive_for(i, 10) ( [&](int value) { results.push_back(value); } );

      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)容。

      上一篇:怎樣把excel表格復(fù)制到word2007
      下一篇:單元格自定義對數(shù)據(jù)體現(xiàn)的優(yōu)勢詳解(單元格的數(shù)據(jù)有效性)
      相關(guān)文章
      在线亚洲精品自拍| mm1313亚洲国产精品美女| 亚洲综合av永久无码精品一区二区| 香蕉视频亚洲一级| 亚洲sm另类一区二区三区| 亚洲精品美女久久久久久久| 亚洲综合精品第一页| 亚洲伊人久久大香线蕉结合| 亚洲xxxxxx| 麻豆狠色伊人亚洲综合网站| 亚洲男人的天堂久久精品| 国产精品亚洲专区在线观看| 四虎必出精品亚洲高清| 亚洲中文无码卡通动漫野外| 亚洲欧美日韩中文字幕在线一区| 亚洲乱码在线卡一卡二卡新区 | 亚洲一区二区三区免费观看| 亚洲一区在线视频| 亚洲一级毛片免观看| 亚洲午夜理论片在线观看| 性色av极品无码专区亚洲| 无码亚洲成a人在线观看| www.91亚洲| 国产亚洲精品成人AA片新蒲金 | 亚洲日韩国产二区无码| 亚洲第一第二第三第四第五第六| 亚洲大尺度无码无码专线一区| 久久亚洲精品成人无码| 亚洲国产午夜中文字幕精品黄网站| 亚洲天堂在线视频| 亚洲一区AV无码少妇电影☆| 亚洲va久久久噜噜噜久久男同| 久久亚洲AV午夜福利精品一区| 综合自拍亚洲综合图不卡区| 亚洲一区二区三区免费观看| 久久精品熟女亚洲av麻豆| 亚洲色偷拍区另类无码专区| 国产亚洲综合色就色| 久久精品国产亚洲av水果派| 亚洲国产电影在线观看| 亚洲欧美精品午睡沙发|