Download - C++ Actor Model - You’ve Got Mail
www.italiancpp.org
C++ Actor ModelYou’ve Got Mail ...
Italian C++ Community
Why are we here?
Italian C++ Community
std::thread/async, isn’t it enough?
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
Italian C++ Community
Concurrency model
- Single Thread → Node.js
- Actor Model → Erlang
- CSP → GO
- STM → Clojure
- etc ...
Italian C++ Community
Node.js
It’s simple
One thread
Asynchronous I/O (event-loop)
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 World\n');}).listen(1337, "127.0.0.1");
console.log('Server running at http://127.0.0.1:1337/');
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 secondsComplete requests: 10000Total transferred: 1130000 bytesRequests 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 maxConnect: 0 58 232.7 0 1000Processing: 5 20 27.1 13 420Waiting: 5 20 27.1 13 420Total: 13 78 246.5 13 1234
Italian C++ Community
Node.js - Is it fast?
YES!!!
Italian C++ Community
Concurrency model
Single Thread → Node.js (Good)
Can we do better?
Copy from others ...
Italian C++ Community
ErlangErlang is a programming language used to build massively scalable soft real-time systems with requirements on high availability.
Italian C++ Community
Actor Model
Italian C++ Community
A model of concurrent computation that treats "actors" as the primitives and fundamental units of computation.
Actor Model
Italian C++ Community
Actor Model
It should embody three properties:
● Processing → Can do something● Storage → Can remember something● Communication → Can communicate with
others
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
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
Italian C++ Community
Actor Model
A1
A3
M1
A21
A22
A23
M2
M3
A2
Italian C++ Community
Actor Model
Does it work?
Is it fast?
Italian C++ Community
Erlang - Resultsab -n 10000 -c 1000 http://127.0.0.1:8080/
Time taken for tests: 0.538 secondsComplete requests: 10000Total transferred: 1300000 bytesRequests 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 maxConnect: 0 10 4.8 10 27Processing: 7 16 4.8 16 39Waiting: 5 13 4.2 13 34Total: 8 26 8.3 26 59
Italian C++ Community
Erlang vs Node.js - ResultsNode.js
Time taken for tests: 1.461 secondsTotal transferred: 1140000 bytesRequests 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 maxConnect: 0 59 234.5 0 1001Processing: 8 22 40.7 14 443Waiting: 7 22 40.7 14 443Total: 10 81 262.9 14 1443
Erlang
Time taken for tests: 0.538 secondsTotal transferred: 1300000 bytesRequests 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 maxConnect: 0 10 4.8 10 27Processing: 7 16 4.8 16 39Waiting: 5 13 4.2 13 34Total: 8 26 8.3 26 59
Italian C++ Community
Actor Model - Is it Fast?
YES!!!
Italian C++ Community
Erlang - Example
https://github.com/extend/cowboy
Italian C++ Community
C++ - Actor model
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
Italian C++ Community
libcppa - Features
● Lightweight actor implementations● Pattern matching for messages ● Error handling based on Erlang’s failure
model● etc ..
Italian C++ Community
libcppa - Is it enough?
Can it solve the initial problem?
example code: double_poll_actor
Italian C++ Community
libcppa - Is it enough?
Main
Poll Left
Poll Right
Acc
poll
poll
at
incLeft
incRight
inc
beast
beast
threshold
fire
fire
Italian C++ Community
libcppa - Actor == thread?
How many actors can I spawn?
A lot!!!!
example code: how_many_actors
Italian C++ Community
libcppa - Errors
● Isolated ● Linked● Monitored
example code: err_mng
Italian C++ Community
libcppa - I/O - brokers
A broker is an event-based actor running in the middleman that multiplexes socket I/O
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
Italian C++ Community
libcppa - Is it fast???
ab -n 10000 -c 1000 http://127.0.0.1:1339/
Time taken for tests: 0.438 secondsComplete requests: 10000Total transferred: 1480000 bytesRequests 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 maxConnect: 1 5 5.6 3 26Processing: 1 6 5.9 4 222Waiting: 1 4 4.8 3 221Total: 3 11 10.6 7 231
Italian C++ Community
libcppa vs Erlang - Resultslibcppa
Time taken for tests: 0.438 secondsTotal transferred: 1480000 bytesRequests 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 maxConnect: 1 5 5.6 3 26Processing: 1 6 5.9 4 222Waiting: 1 4 4.8 3 221Total: 3 11 10.6 7 231
Erlang
Time taken for tests: 0.538 secondsTotal transferred: 1300000 bytesRequests 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 maxConnect: 0 10 4.8 10 27Processing: 7 16 4.8 16 39Waiting: 5 13 4.2 13 34Total: 8 26 8.3 26 59
Italian C++ Community
libcppa - Is it fast???
YES!!!
Italian C++ Community
Actor Model
Gianluca PadovaniSW 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
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
Italian C++ Community
Actor Model
Thank You!