C++ Actor Model - You’ve Got Mail ...

3,116 views

Published on

Can we move beyond threads and locks to manage concurrency? Are there more advanced models than threads and locks? How do other languages manage the concurrency?
We see some examples in others languages and a possible solution in C++.

Example code: https://github.com/italiancpp/meetup-milano-2014/tree/master/cpp_actor_model

0 Comments
15 Likes
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
3,116
On SlideShare
0
From Embeds
0
Number of Embeds
21
Actions
Shares
0
Downloads
52
Comments
0
Likes
15
Embeds 0
No embeds

No notes for slide

C++ Actor Model - You’ve Got Mail ...

  1. 1. www.italiancpp.org C++ Actor Model You’ve Got Mail ...
  2. 2. Italian C++ Community Why are we here?
  3. 3. Italian C++ Community std::thread/async, isn’t it enough?
  4. 4. Italian C++ Community std::thread/async, isn’t it enough? Example code: double_poll Poll Left Poll Right Thread 1 Thread 2 Acc Increment Add Add
  5. 5. Italian C++ Community Concurrency model - Single Thread → Node.js - Actor Model → Erlang - CSP → GO - STM → Clojure - etc ...
  6. 6. Italian C++ Community Node.js It’s simple One thread Asynchronous I/O (event-loop)
  7. 7. Italian C++ Community Node.js - Examples var fs = require("fs"); fs.readFile('/etc/passwd', function (err, data) { if (err) throw err; console.log("-> " + data + " <-"); }); ---- var http = require('http'); http.createServer(function (req, res) { res.writeHead(200, {'Content-Type': 'text/plain'}); res.end('Hello Worldn'); }).listen(1337, "127.0.0.1"); console.log('Server running at http://127.0.0.1:1337/');
  8. 8. Italian C++ Community Node.js - Is it fast? ab -n 10000 -c 1000 http://127.0.0.1:1337/ Time taken for tests: 1.283 seconds Complete requests: 10000 Total transferred: 1130000 bytes Requests per second: 7797.21 [#/sec] (mean) Time per request: 128.251 [ms] (mean) Time per request: 0.128 [ms] (mean, across all concurrent requests) Transfer rate: 860.43 [Kbytes/sec] received Connection Times (ms) min mean[+/-sd] median max Connect: 0 58 232.7 0 1000 Processing: 5 20 27.1 13 420 Waiting: 5 20 27.1 13 420 Total: 13 78 246.5 13 1234
  9. 9. Italian C++ Community Node.js - Is it fast? YES!!!
  10. 10. Italian C++ Community Concurrency model Single Thread → Node.js (Good) Can we do better? Copy from others ...
  11. 11. Italian C++ Community Erlang Erlang is a programming language used to build massively scalable soft real-time systems with requirements on high availability.
  12. 12. Italian C++ Community Actor Model
  13. 13. Italian C++ Community A model of concurrent computation that treats "actors" as the primitives and fundamental units of computation. Actor Model
  14. 14. Italian C++ Community Actor Model It should embody three properties: ● Processing → Can do something ● Storage → Can remember something ● Communication → Can communicate with others
  15. 15. Italian C++ Community Actor Model In an actors system: ● Everything is an actor ● An actor is an entity that sends, receives messages ● An actor is an entity with a behaviour
  16. 16. Italian C++ Community In response to a message that it receives, an actor can: ● create more actors ● send messages to other actors ● determine how to respond to the next message received Actor Model
  17. 17. Italian C++ Community Actor Model A1 A3 M1 A21 A22 A23 M2 M3 A2
  18. 18. Italian C++ Community Actor Model Does it work? Is it fast?
  19. 19. Italian C++ Community Erlang - Results ab -n 10000 -c 1000 http://127.0.0.1:8080/ Time taken for tests: 0.538 seconds Complete requests: 10000 Total transferred: 1300000 bytes Requests per second: 18604.20 [#/sec] (mean) Time per request: 53.751 [ms] (mean) Time per request: 0.054 [ms] (mean, across all concurrent requests) Transfer rate: 2361.86 [Kbytes/sec] received Connection Times (ms) min mean[+/-sd] median max Connect: 0 10 4.8 10 27 Processing: 7 16 4.8 16 39 Waiting: 5 13 4.2 13 34 Total: 8 26 8.3 26 59
  20. 20. Italian C++ Community Erlang vs Node.js - Results Node.js Time taken for tests: 1.461 seconds Total transferred: 1140000 bytes Requests per second: 6843.55 [#/sec] Time per request: 146.123 [ms] Time per request: 0.146 [ms] Transfer rate: 761.88 [Kbytes/sec] Connection Times (ms) min mean[+/-sd] median max Connect: 0 59 234.5 0 1001 Processing: 8 22 40.7 14 443 Waiting: 7 22 40.7 14 443 Total: 10 81 262.9 14 1443 Erlang Time taken for tests: 0.538 seconds Total transferred: 1300000 bytes Requests per second: 18604.20 [#/sec] Time per request: 53.751 [ms] Time per request: 0.054 [ms] Transfer rate: 2361.86 [Kbytes/sec] Connection Times (ms) min mean[+/-sd] median max Connect: 0 10 4.8 10 27 Processing: 7 16 4.8 16 39 Waiting: 5 13 4.2 13 34 Total: 8 26 8.3 26 59
  21. 21. Italian C++ Community Actor Model - Is it Fast? YES!!!
  22. 22. Italian C++ Community Erlang - Example https://github.com/extend/cowboy
  23. 23. Italian C++ Community C++ - Actor model
  24. 24. Italian C++ Community C++ library ● http://neverlord.github.io/libcppa/ - Erlang inspired ● http://www.theron-library.com/ - No fault - No Event IO ● https://code.google.com/p/actor-cpp/source/list - Not developed since 2012 ● https://code.google.com/p/libactor/ - Right now it is usable, although it may not be ready for production ● https://casablanca.codeplex.com/ - Removed actor?!?! ● http://www.stdthread.co.uk/ - No OSS
  25. 25. Italian C++ Community libcppa - Features ● Lightweight actor implementations ● Pattern matching for messages ● Error handling based on Erlang’s failure model ● etc ..
  26. 26. Italian C++ Community libcppa - Is it enough? Can it solve the initial problem? example code: double_poll_actor
  27. 27. Italian C++ Community libcppa - Is it enough? Main Poll Left Poll Right Acc poll poll at incLeft incRight inc beast beast threshold fire fire
  28. 28. Italian C++ Community libcppa - Actor == thread? How many actors can I spawn? A lot!!!! example code: how_many_actors
  29. 29. Italian C++ Community libcppa - Errors ● Isolated ● Linked ● Monitored example code: err_mng
  30. 30. Italian C++ Community libcppa - I/O - brokers A broker is an event-based actor running in the middleman that multiplexes socket I/O
  31. 31. Italian C++ Community libcppa - I/O - brokers ● Create in particular way ● Receive special messages from “system” ● Can take ownership of given connection example code: http_actor
  32. 32. Italian C++ Community libcppa - Is it fast??? ab -n 10000 -c 1000 http://127.0.0.1:1339/ Time taken for tests: 0.438 seconds Complete requests: 10000 Total transferred: 1480000 bytes Requests per second: 22848.00 [#/sec] (mean) Time per request: 43.767 [ms] (mean) Time per request: 0.044 [ms] (mean, across all concurrent requests) Transfer rate: 3302.25 [Kbytes/sec] received Connection Times (ms) min mean[+/-sd] median max Connect: 1 5 5.6 3 26 Processing: 1 6 5.9 4 222 Waiting: 1 4 4.8 3 221 Total: 3 11 10.6 7 231
  33. 33. Italian C++ Community libcppa vs Erlang - Results libcppa Time taken for tests: 0.438 seconds Total transferred: 1480000 bytes Requests per second: 22848.00 [#/sec] Time per request: 43.767 [ms] Time per request: 0.044 [ms] Transfer rate: 3302.25 [Kbytes/sec] Connection Times (ms) min mean[+/-sd] median max Connect: 1 5 5.6 3 26 Processing: 1 6 5.9 4 222 Waiting: 1 4 4.8 3 221 Total: 3 11 10.6 7 231 Erlang Time taken for tests: 0.538 seconds Total transferred: 1300000 bytes Requests per second: 18604.20 [#/sec] Time per request: 53.751 [ms] Time per request: 0.054 [ms] Transfer rate: 2361.86 [Kbytes/sec] Connection Times (ms) min mean[+/-sd] median max Connect: 0 10 4.8 10 27 Processing: 7 16 4.8 16 39 Waiting: 5 13 4.2 13 34 Total: 8 26 8.3 26 59
  34. 34. Italian C++ Community libcppa - Is it fast??? YES!!!
  35. 35. Italian C++ Community Actor Model Gianluca Padovani SW craftsmanship, TDD addicted, agile coach. I like Ruby, NodeJs and everything is interesting. I also work a lot on C#, C++. I like to code, a lot :-) https://www.linkedin.com/pub/gianluca-padovani/2/261/a92 https://twitter.com/GPad619 https://github.com/gpad http://www.slideshare.net/gpadovani
  36. 36. Italian C++ Community Reference ● Carl Hewitt's Homepage ● Hewitt, Meijer and Szyperski: The Actor Model ● Takeaways from Hewitt, Meijer and Szyperski’s talk on the Actor model ● https://github.com/Neverlord/libcppa ● http://libcppa.blogspot.de/ ● Dominik Charousset and Matthias Vallentin: libcppa -- Designing an Actor Semantic for C++11 ● https://github.com/mavam/vast ● Learn you some Erlang ● https://github.com/extend/cowboy ● Actors are not a good concurrency model ● Seven Languages in Seven Weeks: A Pragmatic Guide to Learning Programming Languages
  37. 37. Italian C++ Community Actor Model Thank You!

×