ia+ threading

29
SCALING UP 1

Upload: cyrille-dupuydauby

Post on 05-Jun-2015

61 views

Category:

Technology


0 download

DESCRIPTION

Draft presentation for an event driven multithreading library

TRANSCRIPT

Page 1: Ia+ threading

SCALING UP

�1

Page 2: Ia+ threading

HOW TO SCALE UP SAFELY

NEW PARADIGMS

USAGE OF IA+ THREADING

WHAT YOU WILL LEARN TODAY

�2

Page 3: Ia+ threading

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

Page 4: Ia+ threading

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

Page 5: Ia+ threading

First things first

Parallel computing

Form of computing in which calculations are carried out simultaneously

Implies that multiple processors are available

�5

Page 6: Ia+ threading
Page 7: Ia+ threading

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

Page 8: Ia+ threading

IT IS HARD

�8

Page 9: Ia+ threading

HOWEVER EXPERIENCED YOU

MAY BE

�9

Page 10: Ia+ threading

INCREDIBLY HARD

�10

Page 11: Ia+ threading

!//  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

Page 12: Ia+ threading

THIS ONE WAS EASY

In the wild, those lines would be mixed with a lot t business logics and scaffolding code

!

�12

Page 13: Ia+ threading

NOT CONVINCED YET?

�13

Page 14: Ia+ threading

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

Page 15: Ia+ threading

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

Page 16: Ia+ threading

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

Page 17: Ia+ threading

THERE MUST BE SOMETHING ELSE

�17

Page 18: Ia+ threading

There is a solution in C#

It avoid locks altogether

It scales well

It does not require full brain rewiring

�18

Page 19: Ia+ threading

IA+ Threading

Page 20: Ia+ threading

Let’s talk patterns

Page 21: Ia+ threading

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

Page 22: Ia+ threading

Feedback

Run #1 was chaotic while run #2 was smoother

We just went from mutable to immutable

Page 23: Ia+ threading

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!

Page 24: Ia+ threading

You just demonstrated lock free concurrency

Congrats!

Page 25: Ia+ threading

What about causality?

How can we ensure events are properly taken into accounts?

By the way, what do we mean by 'properly'?

Page 26: Ia+ threading

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

Page 27: Ia+ threading

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

Page 28: Ia+ threading

Order of magnitude

Page 29: Ia+ threading

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