ia+ threading
DESCRIPTION
Draft presentation for an event driven multithreading libraryTRANSCRIPT
SCALING UP
�1
HOW TO SCALE UP SAFELY
NEW PARADIGMS
USAGE OF IA+ THREADING
WHAT YOU WILL LEARN TODAY
�2
ME, MYSELF and I
Been doing multithreading and related stuff for 20 years, 2/3 C++ 1/3 C#
components and libraries
debugging/revamped applications
Have a blog on it
Proud Father of IA+ Threading (© SGCIB)
�3
First things first
Concurrent computing
Form of computing in which programs are designed as collections of interacting processes
no relation to the hardware it is running on
�4
First things first
Parallel computing
Form of computing in which calculations are carried out simultaneously
Implies that multiple processors are available
�5
The Free Lunch is over
Famous speech (2005) where Herb Sutter stressed out that
CPU Frequency was no longer progressing
Developers have to face concurrent programming to expect to benefit from newer CPU
but concurrent programming is hard
�7
IT IS HARD
�8
HOWEVER EXPERIENCED YOU
MAY BE
�9
INCREDIBLY HARD
�10
!// Let's start easy to put things in motion internal class DeadLock1 { private Thread _firstRunner; private Thread _secondRunner; private bool _stopSignal = false; private readonly object _synchro = new object(); ! public DeadLock1() { _firstRunner = new Thread(FirstRunner); _secondRunner = new Thread(SecondRunner); } ! // start your engines public void Start() { _firstRunner.Start(); _secondRunner.Start(); Thread.Sleep(100); lock (_synchro) { _stopSignal = true; Monitor.Pulse(_synchro); } _firstRunner.Join(); _secondRunner.Join(); } !!!! // first thread logic private void FirstRunner()
{ lock (_synchro) { if (!_stopSignal) { Monitor.Wait(_synchro); } } } // second thread logic private void SecondRunner() { lock (_synchro) { if (!_stopSignal) { Monitor.Wait(_synchro); } } } }
class Program { static void Main(string[] args) { DeadLock1 sample1 = new DeadLock1(); sample1.Start(); } }
�11
THIS ONE WAS EASY
In the wild, those lines would be mixed with a lot t business logics and scaffolding code
!
�12
NOT CONVINCED YET?
�13
class C!{! static C() ! {! // Let's run the initialization! // on another thread!! var thread = new Thread(Initialize);! thread.Start();! thread.Join();! }! static void Initialize() { }! static void Main() { }!}!
�14
IT'S F*CKING HARD
You also need to master the threading model of the CLR
Of course you need to master the threading model of libraries
When was the last time you saw and read a documented threading model?
�15
CAN'T GO BACK the free lunch is over
No complete solutions
RX, TPL: still requires lock
Clojure, ErLang: requires rewiring brain
GO: blocking primitives
Scala, F#: functional+ no thread safety
�16
THERE MUST BE SOMETHING ELSE
�17
There is a solution in C#
It avoid locks altogether
It scales well
It does not require full brain rewiring
�18
IA+ Threading
Let’s talk patterns
Immutable State
State is a mutable entity by definition
But can be stored in a Value object
Altering the state means creating a new version of the state
Immutability is good for sharing
Feedback
Run #1 was chaotic while run #2 was smoother
We just went from mutable to immutable
Message passing
We also passed around the state of the object as a message
Originaly used for commands but works well with state as well
Since state is preserved, no need for copies!
You just demonstrated lock free concurrency
Congrats!
What about causality?
How can we ensure events are properly taken into accounts?
By the way, what do we mean by 'properly'?
Causality
1. Events can be totally ordered for any given objects (source or dest)
2. There may not a be a total order for all events and for all objects
Relativiy
Thanks to relativity, we know that any two events can be SEEN in any order
As long as they do not occur at the same location
Order of magnitude
Pragmatic view
Events must be kept in their natural local order
Generated events also must be kept in the order they were generated
General ordering must remain acceptable for a human