项目作者: PierreKieffer

项目描述 :
Actor Model
高级语言: C++
项目地址: git://github.com/PierreKieffer/actor-system.git
创建时间: 2021-07-06T14:12:01Z
项目社区:https://github.com/PierreKieffer/actor-system

开源协议:

下载


actor-system

C++ implementation of an actor model.


The Actor Model provides a higher level of abstraction for writing concurrent and distributed systems.

In response to a message it receives, an actor can: make local decisions, create more actors, send more messages, and determine how to respond to the next message received.

Actors may modify their own private state, but can only affect each other indirectly through messaging.

Actor

  1. #include "actor.hpp"
  2. int main(){
  3. Actor actor;
  4. return 0;
  5. };

Buffer

Each actor instance has a message buffer allowing to receive events from other actors, or from itself.

Consumer

The consumer method of an actor allows to read and process (with a custom function) the new messages stored in its buffer.

Standard consumer

To start a consumer in a new thread :

  1. #include <iostream>
  2. #include <thread>
  3. #include <sstream>
  4. #include "actor.hpp"
  5. void customConsumerFunc(std::string payload){
  6. std::stringstream out;
  7. out << "Consumer data processing : " << payload;
  8. std::cout << out.str() << std::endl;
  9. };
  10. int main(){
  11. Actor actor;
  12. // Consumer Thread
  13. std::thread consumerThread([&](){
  14. actor.consumer(customConsumerFunc);
  15. });
  16. consumerThread.join();
  17. return 0;
  18. };

Acknowledge a message

The consumer method can be overloaded in order to send back an acknowledgment after the processing of each message.

The acknowledgment message is sent to an actor’s buffer.

  1. #include <iostream>
  2. #include <thread>
  3. #include <sstream>
  4. #include "actor.hpp"
  5. void customConsumerFunc(std::string payload){
  6. std::stringstream out;
  7. out << "Consumer data processing : " << payload;
  8. std::cout << out.str() << std::endl;
  9. };
  10. int main(){
  11. Actor actor_1;
  12. Actor actor_2;
  13. // actor_2 Consumer Thread, with acknowledgement to actor_1
  14. std::thread consumerThread([&](){
  15. actor_2.consumer(customConsumerFunc, actor_1);
  16. });
  17. consumerThread.join();
  18. return 0;
  19. };

Default acknowledgment message value is __ACK__.

To use your own acknowledgment message :

  1. actor_2.consumer(customConsumerFunc, actor_1, "message received");

Producer

The producer method of an actor allows to send a message to the buffer of an other actor, or to its own buffer.

To start a producer in a new thread :

  1. #include <iostream>
  2. #include <thread>
  3. #include <sstream>
  4. #include "actor.hpp"
  5. int main(){
  6. Actor actor_1;
  7. Actor actor_2;
  8. // Producer Thread, to send a message to actor_2
  9. std::thread producerThread([&](){
  10. std::string payload = "{\"foo\" : \"bar\"}";
  11. actor_1.producer(actor_2, payload);
  12. });
  13. producerThread.join();
  14. return 0;
  15. };

Full example

  1. #include <iostream>
  2. #include <sstream>
  3. #include <unistd.h>
  4. #include <thread>
  5. #include "actor.hpp"
  6. // actor_1 custom consumer
  7. void actor_1CustomConsumerFunc(std::string payload){
  8. std::stringstream out;
  9. out << "actor_1 Consumer data processing : " << payload;
  10. std::cout << out.str() << std::endl;
  11. };
  12. // actor_2 custom consumer
  13. void actor_2CustomConsumerFunc(std::string payload){
  14. std::stringstream out;
  15. out << "actor_2 Consumer data processing : " << payload;
  16. std::cout << out.str() << std::endl;
  17. };
  18. int main(){
  19. Actor actor_1;
  20. Actor actor_2;
  21. // actor_2 Consumer Thread, with acknowledgment to actor_1
  22. std::thread actor_2ConsumerThread([&](){
  23. actor_2.consumer(actor_2CustomConsumerFunc, actor_1, "ack from actor_2");
  24. });
  25. // actor_1 Consumer Thread
  26. std::thread actor_1ConsumerThread([&](){
  27. actor_1.consumer(actor_1CustomConsumerFunc);
  28. });
  29. // actor_1 Producer Thread
  30. std::thread actor_1ProducerThread([&](){
  31. std::string payload = "{\"foo\" : \"bar\"}";
  32. actor_1.producer(actor_2, payload);
  33. });
  34. actor_2ConsumerThread.join();
  35. actor_1ConsumerThread.join();
  36. actor_1ProducerThread.join();
  37. return 0;
  38. };

Build :

  1. g++ -pthread *.cpp -o actor-system

Run :

  1. ./actor-system

Output :

  1. actor_2 Consumer data processing : {"foo" : "bar"}
  2. actor_1 Consumer data processing : ack from actor_2