- 28. Parallel Programming II
C++ Threads, Shared Memory, Concurrency, Excursion: lock algorithm (Peterson), Mutual Exclusion Race Conditions [C++ Threads: Anthony Williams, C++ Concurrency in Action]
841
28. Parallel Programming II C++ Threads, Shared Memory, Concurrency, - - PowerPoint PPT Presentation
28. Parallel Programming II C++ Threads, Shared Memory, Concurrency, Excursion: lock algorithm (Peterson), Mutual Exclusion Race Conditions [C++ Threads: Anthony Williams, C++ Concurrency in Action ] 841 C++11 Threads #include <iostream>
841
create thread hello join
842
create threads join
843
hello from main hello from 2 hello from 1 hello from 0
hello from 1 hello from main hello from 0 hello from 2
hello from main hello from 0 hello from hello from 1 2
844
845
More background and details in chapter 2 of the book C++ Concurrency in Action, Anthony Williams, Manning 2012. also available online at the ETH library.
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
The code assumes that the access to flag / victim is atomic and particularly lineariz- able or sequential consistent. An assump- tion that – as we will see below – is not nec- essarily given for normal variables. The Peterson-lock is not used on modern hard- ware.
36not relevant for the exam 877
878
acquire_mutex(); // entry algorithm\\ ... // critical section release_mutex(); // exit algorithm
879
880
881
882
Call of lock: counter incremented Call of unlock: counter is decremented. If counter = 0 the lock is released.
883
884
885
886
887
888
889
890
891
892
893
894
A B C D Can this fail? There is no interleaving of f and g that would cause the assertion to fail: A B C D A C B D A C D B C A B D C C D B C D A B It can nevertheless fail!
895
sequentially equivalent
896
897
898
if equal always
899
900
slow,high latency,low cost,high capacity fast,low latency, high cost, low capacity
901
902
903
904
905
906