Modern C++
Old Dog, New Tricks
Todd L. Montgomery @toddlmontgomery
StoneTor
Modern C++ Old Dog, New Tricks Todd L. Montgomery @toddlmontgomery - - PowerPoint PPT Presentation
StoneTor Modern C++ Old Dog, New Tricks Todd L. Montgomery @toddlmontgomery C++ is so old Languages are Tools Learning Tools is Good There are only two kinds of languages: the ones people complain about and the ones nobody uses.
Modern C++
Old Dog, New Tricks
Todd L. Montgomery @toddlmontgomery
StoneTor
Languages are Tools Learning Tools is Good
“There are only two kinds of languages: the ones people complain about and the ones nobody uses.”
— Bjarne Stroustrup
Feature Bloat & Complexity
Not Fast Enough
Low & Predictable Latency is key
We are in a new world
Multi-core, Multi-socket, Cloud...
We are in a new world
UDP, IPC, InfiniBand, RDMA, PCI-e Multi-core, Multi-socket, Cloud...
Aeron is trying a new approach
The Team
Todd Montgomery Richard Warburton Martin Thompson
Publishers Subscribers Channel Stream
Messaging
Channel
A library, not a framework, on which other abstractions and applications can be built
Composable Design
OSI layer 4 Transport for message oriented streams
OSI Layer 4 (Transport) Services
Publisher Subscriber Subscriber Publisher
Architecture
IPC Log Buffer
Sender Receiver Receiver Sender Publisher Subscriber Subscriber Publisher
Architecture
Media IPC Log Buffer Media (UDP, InfiniBand, PCI-e 3.0)
Conductor Sender Receiver Conductor Receiver Sender Publisher Subscriber Subscriber Publisher Admin Events
Architecture
Admin Events Media IPC Log Buffer Media (UDP, InfiniBand, PCI-e 3.0) Function/Method Call Volatile Fields & Queues
Client Media Driver Media Driver Conductor Sender Receiver Conductor Receiver Sender Client Publisher Conductor Conductor Subscriber Subscriber Publisher Admin Events
Architecture
Admin Events Media IPC Log Buffer IPC Ring/Broadcast Buffer Media (UDP, InfiniBand, PCI-e 3.0) Function/Method Call Volatile Fields & Queues
Client Media Driver Media Driver Conductor Sender Receiver Conductor Receiver Sender Client Publisher Conductor Conductor Subscriber Subscriber Publisher Admin Events
Architecture
Admin Events Media IPC Log Buffer IPC Ring/Broadcast Buffer Media (UDP, InfiniBand, PCI-e 3.0) Function/Method Call Volatile Fields & Queues
Client Media Driver Media Driver Conductor Sender Receiver Conductor Receiver Sender Client Publisher Conductor Conductor Subscriber Subscriber Publisher Admin Events
Architecture
Admin Events Media IPC Log Buffer IPC Ring/Broadcast Buffer Media (UDP, InfiniBand, PCI-e 3.0) Function/Method Call Volatile Fields & Queues Client
Asynchrony
Asynchrony
http://reactivex.io/
Challenges
Dealing with Back Pressure
http://www.reactive-streams.org/
Rx Heterogeneous Connectivity
http://reactivesocket.io/
Rx Heterogeneous Connectivity
http://reactivesocket.io/
Protocol
“Within C++, there is a much smaller and cleaner language struggling to get out.” ”And no, that smaller and cleaner language is not Java or C#.”
— Bjarne Stroustrup
Modern C++
Resource Ownership & Lifetime
Modern C++
std::lock_guard std::unique_ptr std::shared_ptr
Resource Acquisition is Initialization (RAII)
http://en.cppreference.com/w/cpp/language/raii
Other C++ Idioms
* Substitution Failure is Not An Error
Modern C++
std::shared_ptr std::unique_ptr std::weak_ptr
Smart Pointers
http://en.cppreference.com/w/cpp/memory
Smart Pointers
Smart Pointers & Ownership
Modern C++
Lambda’s & Function Objects
Modern C++
std::atomic<bool> std::atomic_flag std::atomic<int>
Atomic Operations
http://en.cppreference.com/w/cpp/atomic
Modern C++
std::thread std::mutex std::async std::promise std::future
Thread Support
http://en.cppreference.com/w/cpp/thread
Thread Support
Modern C++
Moving vs. Copying Ownership?
Move Construction/Assignment
http://en.cppreference.com/w/cpp/language/rule_of_three
Modern C++
CMake Google Test Google Mock Google Benchmark etc.
Modern Toolchain
https://cmake.org/ https://github.com/google/googletest
Lessons - What do you think?
Lessons
RAII & Smart Pointers
But But But… CYCLES!!!
Smart Pointers
Dealing with “Cycles”
(GCPP)
https://github.com/hsutter/gcpp
RAII & Smart Pointers
RAII & Smart Pointers
RAII & Smart Pointers
Hiding Complexity is not Dealing with Complexity
Explicit Coupling / Scope / Dependency
Speaks to Quality Build Quality In
Explicit Coupling / Scope / Dependency
Enforced Decoupling & Isolation….
Coupling / Scope / Dependency
What a language provides? What a language takes away?
More Important?
Deal with it up front Pays off massively later “If it hurts, do it more often”
Explicit Coupling / Scope / Dependency
Lessons
Not Needed Because Escape Analysis Value Types … ${EXCUSE}[i]
Stack Allocation (Lack of)
Mechanism is NOT a set of “features”
Stack Allocation
Stack Function Objects very very low cost deferred/conditional execution
Stack Allocation
Lessons
Move Construction/Assignment
Move Construction/Assignment
Move Construction/Assignment
Move Construction/Assignment
Lessons
Client Media Driver Media Driver Conductor Sender Receiver Conductor Receiver Sender Client Publisher Conductor Conductor Subscriber Subscriber Publisher Admin Events
Architecture
Admin Events Media IPC Log Buffer IPC Ring/Broadcast Buffer Media (UDP, InfiniBand, PCI-e 3.0) Function/Method Call Volatile Fields & Queues
Data Structures (Shared Memory)
Creates a
replicated persistent log
What Aeron does
Log Buffer File
Term 2 Log Meta Data
Atomic & Ordered Operations
Term 1 Term 0
Position
Unique identification of a byte within each stream
Publishers, Senders, Receivers, and Subscribers all keep position counters
Position counters are the key to flow control and monitoring
Statistics & Position Counters are accessible in shared memory
Atomic & Ordered Operations
Multiple Challenges
Size Matters (Atomics)
Memory Models
So… Aeron uses its own atomic
(JMM compatible)
* gcc x86_64 initially contributed by phaynes (phaynes@threatmetrix.com)
Languages are Tools Fit for purpose Add to your toolbox!
“There are more useful systems developed in languages deemed awful than in languages praised for being beautiful--many more.”
— Bjarne Stroustrup (via Jason Pontin)
https://github.com/real-logic/Aeron
Where can I find them?
https://github.com/ReactiveSocket/ reactivesocket-cpp
Aeron: https://github.com/real-logic/Aeron ReactiveSocket: https://github.com/ReactiveSocket/reactivesocket-cpp Twitter: @toddlmontgomery
Thank You!
Questions?