Скачать презентацию C Memory Model What is the memory Скачать презентацию C Memory Model What is the memory

5. C# Memory Model.pptx

  • Количество слайдов: 17

C# Memory Model C# Memory Model

What is the memory model Memory model describes the interactions of threads through memory What is the memory model Memory model describes the interactions of threads through memory and their shared use of the data.

Sequential consistency memory model Sequential consistency is order in which a single thread issues Sequential consistency memory model Sequential consistency is order in which a single thread issues method calls in program order. The simplest memory model.

Real situation ● ● ● speculative execution out-of-order execution CPU caches compiler optimizations runtime(JIT) Real situation ● ● ● speculative execution out-of-order execution CPU caches compiler optimizations runtime(JIT) optimizations

Synchronization 1. Lock structures lock statement, Monitor, Reader. Writer. Lock. Slim others classes 2. Synchronization 1. Lock structures lock statement, Monitor, Reader. Writer. Lock. Slim others classes 2. CAS System. Threading. Interlocked class 3. Volatile fields volatile modifier

Volatile fields C# LANGUAGE SPECIFICATION(ECMA-334): When a field-declaration includes a volatile modifier, the fields Volatile fields C# LANGUAGE SPECIFICATION(ECMA-334): When a field-declaration includes a volatile modifier, the fields introduced by that declaration are volatile fields. private volatile int v. Int;

Volatile fields and optimizations C# LANGUAGE SPECIFICATION(ECMA-334): For non-volatile fields, optimization techniques that reorder Volatile fields and optimizations C# LANGUAGE SPECIFICATION(ECMA-334): For non-volatile fields, optimization techniques that reorder instructions can lead to unexpected and unpredictable results in multi-threaded programs that access fields without synchronization such as that provided by the lockstatement (§ 15. 12). These optimizations can be performed by the compiler, by the runtime system, or by hardware. For volatile fields, such reordering optimizations are restricted

Think a bit public class Init { private int _data = 0; private bool Think a bit public class Init { private int _data = 0; private bool _initialized = false; void Init() { _data = 42; _initialized = true; } void Print() { if (_initialized) Console. Write. Line(_data); else Console. Write. Line("Not initialized"); } } Possible results: 1. 42 2. 0 3. Not initialized

Think a bit public class Loop. Test { private bool _flag = true; public Think a bit public class Loop. Test { private bool _flag = true; public void Run() { new Thread( () => { _flag = false; } ). Start(); while (_flag) {} } } Thanks to our compiler we may see: public void Run() { new Thread( () => { _flag = false; } ). Start(); if (_flag) { while (true){} } } public void Run() { new Thread( () => { _flag = false; } ). Start(); bool f =_flag; while (f){} }

Volatile reads Acquire semantic ⇒ reads and writes cannot move before a volatile read. Volatile reads Acquire semantic ⇒ reads and writes cannot move before a volatile read. public volatile int _data; public bool _initialized; public void Do. Read() { bool init = _initialized; int i = _data; _initialized = true; } public void Do. Read() { int i = _data; bool init = _initialized; _initialized = true; }

Think a bit(acquire semantic) public class Acquire. Semantics. Example { private int _a; private Think a bit(acquire semantic) public class Acquire. Semantics. Example { private int _a; private volatile int _vb; private int _c; void Foo() { int a = _a; // Read 1 int b = _vb; // Read 2 (volatile) int c = _c; // Read 3 } } int a = _a; // Read 1 int b = _b; // Read 2 (volatile) int a = _a; // Read 1 int c = _c; // Read 3 int a = _a; // Read 1

Volatile writes Release semantic ⇒ reads and writes cannot move after a volatile write. Volatile writes Release semantic ⇒ reads and writes cannot move after a volatile write. public volatile int _data; public bool _initialized; public void Do. Write() { _initialized = true; _data = 42; bool init = _initialized; } public void Do. Write() { _initialized = true; bool init = _initialized; _data = 42; }

Think a bit(release semantic) public class Release. Semantics. Example { private int _a; private Think a bit(release semantic) public class Release. Semantics. Example { private int _a; private volatile int _vb; private int _c; void Foo() { _a = 1; // Write 1 _vb = 1; // Write 2 (volatile) _c = 1; // Write 3 } } _a = 1; // Write 1 _c = 1; // Write 3 _b = 1; // Write 2 (volatile) _c = 1; // Write 3 _a = 1; // Write 1 _c = 1; // Write 3 _b = 1; // Write 2 (volatile)

Write atomicity C# ECMA specification guarantees write atomicity for: ● reference types ● short Write atomicity C# ECMA specification guarantees write atomicity for: ● reference types ● short ● bool ● ushort ● char ● uint ● byte ● int ● sbyte ● float Writes to long, double, decimal, struct NOT atomic and hence cannot be marked as volatile.

Safe publication public class Unsafe. Local. DCL { public class Safe. Local. DCL { Safe publication public class Unsafe. Local. DCL { public class Safe. Local. DCL { private Singleton instance; private volatile Singleton instance; public Singleton get. Instance() { Singleton res = instance; if (res == null) { lock(this) { res = instance; if (res == null) { res = new Singleton(); instance = res; } } } return res; } }

Safe publication class Boxed. Int { public int Value { get; set; } } Safe publication class Boxed. Int { public int Value { get; set; } } partial class Lazy. Init { public void Print() { var b = _box; if (b != null) { Console. Write. Line("Value: {0}", partial class Lazy. Init { private Boxed. Int _box; public void Init() { var b = _box; if (b == null) { lock(this) { b = new Boxed. Int(); b. Value = 42; _box = b; b. Value); } else } } { Console. Write. Line("Not set"); } } }

Further readings 1. 2. 3. 4. ECMA-334 ECMA-335 https: //msdn. microsoft. com/en-us/magazine/jj 863136. aspx Further readings 1. 2. 3. 4. ECMA-334 ECMA-335 https: //msdn. microsoft. com/en-us/magazine/jj 863136. aspx https: //msdn. microsoft. com/en-us/magazine/jj 883956. aspx Java materials: 1. http: //shipilev. net/ 2. The Art of Multiprocessor Programming by Maurice Herlihy, Nir Shavit 3. http: //g. oswego. edu/ - Doug Lea Books: 1. Computer Architecture, Fifth Edition: A Quantitative Approach (The Morgan Kaufmann Series in Computer Architecture and Design)