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
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. 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
11. Italian C++ Community
Erlang
Erlang is a programming language used to build massively
scalable soft real-time systems with requirements on high
availability.
13. Italian C++ Community
A model of concurrent computation
that treats "actors" as the primitives
and fundamental units of computation.
Actor Model
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. 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. 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
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. 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
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. Italian C++ Community
libcppa - Features
● Lightweight actor implementations
● Pattern matching for messages
● Error handling based on Erlang’s failure
model
● etc ..
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. 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. 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. 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
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. 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