There is a nullable variable, which requires access from different threads. For example, int? . What method of synchronization of such a variable should be used in C #? The most straightforward is to wrap it all in a lock . There was an assumption that volatile will cope with it, but it does not know how to c nullable .
What if you need to copy this value locally, write a function (lambda) whose body is wrapped in a lock ? Copying primarily in order to reduce the blocking time between threads.
lock. Other methods are allowed to use if you need extreme optimization, and you understand very well what you are doing. (In particular, if you do not have dependent variables.) - VladDint? local; lock (mutex) local = shared; // используем localint? local; lock (mutex) local = shared; // используем local- VladD