Multicore
Салат асинхронный
Terminology
Amdahl law: so we have many CPUs
Embarrassingly parallel problems
Hardware: Atomic operations
Hardware: Fences
Hardware: Non-uniform memory architecture
Hardware: Hyper Threading
Hardware: Intel’s Transactional Synchronization Extensions
Spinlock\Futex\Crit Section\Fast Mutual Exclusion
Thread pool pattern
Reader-writer lock pattern
Event pattern
Semaphore
Double-checked locking
Striped locking
Pulse\Wait
Producer-consumer synchronization
Futures and promises
Futures and promises
volatile semantics
volatile is not atomic
Examples of optimizations prevented by volatile semantics
Memory model
Memory model
Non-blocking algorithms
Lock-free stack: Interlocked Singly Linked List
Lock-free stack: Interlocked Singly Linked List
Purity\Functionality
Purity\Functionality

Salad "Asynchronous"

1. Multicore

[email protected]

2. Салат асинхронный

Салат "Асинхронный": помиогурдоры,цымайон,ез.
Salad "Asynchronous": tomacucumtoes,bersmayonn,aise

3. Terminology

• Concurrency
Computation may interleave with other computations
• Multitasking
Device may execute more than one program at a time
• Parallel execution
Device is capable of advancing more that one computation in a point of time
• Multithreading
Program is represented as a set of worker threads
• Asynchrony
Program contain non-blocking calls

4. Amdahl law: so we have many CPUs

5. Embarrassingly parallel problems

• Solved mostly by SIMD

6. Hardware: Atomic operations

• Load-link/store-conditional
• Compare-and-swap

7. Hardware: Fences

• SFENCE
• LFENCE
• MFENCE

8. Hardware: Non-uniform memory architecture

9. Hardware: Hyper Threading

10. Hardware: Intel’s Transactional Synchronization Extensions

11. Spinlock\Futex\Crit Section\Fast Mutual Exclusion

• Context of exclusion
• Reliable exclusion mechanics
• Inter-process communication
• Waiting time work (spin\sleep\pump)
• Reentrancy
• Deadlock detection

12. Thread pool pattern

13. Reader-writer lock pattern

• Lock for read (many at a time)
• Lock for write (one at a time, excluding all readers)
• Upgradable: escalate from reader to writer
• Is sync primitives is really so hard?
• .NET 2.0 implementation had a deadlock bug
• Starvation is possible

14. Event pattern

• Simple event
• Autoreset event
• Countdown event \ rundown protection

15. Semaphore

16. Double-checked locking

17. Striped locking

• Used in CuncurrentDictionary

18. Pulse\Wait

19. Producer-consumer synchronization

20. Futures and promises

21. Futures and promises

22. volatile semantics

• Non-volatile write
Volatile write
There is no such thing as thread cache. This is an abstraction over compilers and hardware optimizations.
Source: http://igoro.com/archive/volatile-keyword-in-c-memory-model-explained/

23. volatile is not atomic

• volatile is not atomic
• volatile is not atomic
• volatile is not atomic
• volatile is not atomic
• volatile is not atomic
• volatile is not atomic
• volatile is not atomic
• volatile is not atomic

24. Examples of optimizations prevented by volatile semantics

• Register allocation
• Out-of-order execution
• Loop fusion
• Invariant hoisting
• Rematerialization
• Almost any compiler, JIT or CPU optimization
• https://en.wikipedia.org/wiki/Optimizing_compiler
• https://en.wikipedia.org/wiki/Program_optimization

25. Memory model

• Example: .NET memory model
Java memory model is base on “happens before” memory model
C++ introduced memory model in C++11, most of questions were not even undefined behavior.
There is no such thing as thread cache. This is an abstraction over compilers and hardware optimizations.

26. Memory model

• Search for memory model of your platform\language

27. Non-blocking algorithms

• Obstruction-free
• Lock-free
• Wait-free

28. Lock-free stack: Interlocked Singly Linked List

Based on work by Alex Skidanov https://habrahabr.ru/post/174369/

29. Lock-free stack: Interlocked Singly Linked List

Not lock-free
Segfault
Undefined behavior
ABA
Based on work by Alex Skidanov https://habrahabr.ru/post/174369/ There you’ll find working code
More on lock-free structures and concurrency http://www.1024cores.net/

30. Purity\Functionality

Source: http://www.slideshare.net/mariofusco/why-we-cannot-ignore-functional-programming

31. Purity\Functionality

32.

• Knock knock.
• Race condition.
• Who's there?
English     Русский Правила