Why is the standard C# event invocation pattern thread-safe without a memory barrier or cache invalidation? What about similar code?

后端 未结 5 1816
余生分开走
余生分开走 2021-02-19 03:37

In C#, this is the standard code for invoking an event in a thread-safe way:

var handler = SomethingHappened;
if(handler != null)
    handler(this, e);
         


        
5条回答
  •  佛祖请我去吃肉
    2021-02-19 04:06

    Capturing reference to immutable object guarantees thread safety (in sense of consistency, it does not guarantee that you get the latest value).

    List of events handlers are immutable and thus it is enough for thread safety to capture reference to current value. The whole object would be consistent as it never change after initial creation.

    Your sample code does not explicitly state if Foo is immutable, so you get all sorts of problems with figuring out whether the object can change or not i.e. directly by setting properties. Note that code would be "unsafe" even in single-threaded case as you can't guarantee that particular instance of Foo does not change.

    On CPU caches and like: The only change that can invalidate data at actual location in memory for true immutable object is GC's compaction. That code ensures all necessary locks/cache consistency - so managed code would never observe change in bytes referenced by your cached pointer to immutable object.

提交回复
热议问题