CAF (c + + actor Framework) uses essays (deferred send, message forwarding, message priority) (iv)

Source: Internet
Author: User

e). Message delayed send (not much different from the front directly on the code)

#include <iostream>#include"caf/all.hpp"#include"caf/io/all.hpp"#include<string>#include<chrono>using namespacestd;using namespacecaf;behavior Fun (Event_based_actor*Self ) {    return{[Self] (Const string&str) {aout (self)<<str<<Endl; Auto T2=std::chrono::system_clock::to_time_t (Std::chrono::system_clock::now ()); cout<<"dalay Time:"<<t2<<Endl; Self-quit (); }    };}voidFUN1 (event_based_actor*Self , actor Buddy) { Self->delayed_send (Buddy, Std::chrono::seconds (1),"hi!");}intMain () {Auto Actor1=spawn (fun); Auto T1=std::chrono::system_clock::to_time_t (Std::chrono::system_clock::now ()); cout<<"before Delayed_send:"<<t1<<Endl; Auto Actor2=spawn (Fun1,actor1);    Caf::await_all_actors_done ();    Shutdown (); return 0;}

Result is

f). Message forward (message forwarding) forward.

Put code on it.

#include <iostream>#include"caf/all.hpp"#include"caf/io/all.hpp"#include<string>#include<chrono>using namespacestd;using namespacecaf;behavior fun2 (event_based_actor*Self ) {    return{[Self] (Const string&str) {aout (self)<<"C Get message return to A"<<Endl; Aout (self)<<"C ' s address is:"<<self->address () <<Endl; return "Hello, A"; Self-quit (); }    };} Behavior Fun1 (Event_based_actor* Self,ConstActor &Buddy) {    return {        [=](Const string&str) {aout (self)<<"B Get message forward to C"<<Endl; Self-forward_to (Buddy); Self-quit (); }    };}voidFun (event_based_actor* Self,ConstActor &Buddy) { Self->sync_send (Buddy,"hi!"). then ([=](Const string&str) {aout (self)<<str<<Endl; Aout (self)<<"A think Last_sender is:"<<self->last_sender () <<Endl;    }    ); Aout (self)<<"A Send to b!"<<Endl;}intMain () {Auto Actorc=spawn (FUN2); Auto Actorb=spawn (FUN1,ACTORC); Auto Actora=spawn (Fun,actorb);    Caf::await_all_actors_done ();    Shutdown (); return 0;}

Result is

CAF (c + + actor Framework) uses essays (deferred send, message forwarding, message priority) (iv)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.