Why this presentation???- Something new? No!- Multithreading many years!+ technology, APIsToo little: talk about good patternsWhat I will show today:- Modern APIs are not enough to make safe and friendly classes.- Simple, language independent techniques -> Sleep well
From 1970 ExponentialMy programs run faster every year!(single core)This is called the free lunch!Life is goodSmell roses and liliesAlmost hear spring music
The whole Amdahls law: The speedup of a program using multiple processors in parallel computing is limited by the time needed for the sequential fraction of the program.->Speedup of different algorithms
Speedup of different algorithms.If your algorithm … only small sequential parts … greenIf half … sequential … blueConclusion: Your type of algorithms or problems matter!-> Sequential program
Very simple piece of work
My old program: 15 s
-> Time now
It’s not half of 15, Not double of 15, but even worse!I have not got the faintest clue as to what is going on!We have limited time, so I’ll have to jump to a conclusion: It’s the memory model I don’t have a clue about when I write this code.What is going on?
So, what happened?Static long aImagine a librarian running between the cores managing the loans-> Tweet
Exactly what we experienced
Will this work… always???Who can spot the problem? Race condition: a += temp-> Interlocked/atomic
Release mode – the figures are not as bad-> Time now
-> Active object pattern
-> Let’s try to implement
future.Result returns now or blocks-> internally
PrimeActiveObjectTestOne timeMany in parallel – light up CPU?SorterActiveObjectTestImplTestNiceBreak it – shared data-> Where are we now?
Examples:Process new files arriving on disk, e.g. FileSystemWatcherProcess data arriving on network to even out bursts-> Lock ordering
2 files2 threadseach thread need read/write access to both files for a little while-> Execution pipeline
Remember the pyramids of caches.Nice if the data is cachedIf we have sequence of operations, a pipeline, to perform on the same data, do it on the same thread.