You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Call out thread-safety for access to static class members #10244
It'd be great if this article could also touch on thread-safety when accessing static members; I assume similar to the static class constructor, it will only be initialized at most once. But it'd be nice to have that called out here.
Document Details
⚠ Do not edit this section. It is required for docs.microsoft.com ➟ GitHub issue linking.
ID: 7f20db0d-ef5c-d8e7-2267-0f409c499e0f
Version Independent ID: de2dac73-e810-dd42-f518-e50926b3747f
There is no language level guarantee on thread synchronization for static methods. Whether or not a static method is thread safe depends on the code in the static method. If the static method does not mutate shared data, it would be thread safe. However, any access to shared data would need to be synchronized.
Because there isn't a language guarantee here, we've avoided making any statement because there isn't language guarantee about thread safety for static methods. Any given static method may be thread safe. Or, it may not be. That can only be known by understanding the implementation of the method.
Thanks @BillWagner, I understand that I can always put unsafe code somewhere that does silly things. :)
It's just helpful to know if the underlying construct is thread-safe and is only going to be able to be entered once or if I need to worry about mutexing around initializing/accessing the member or not.
After some internal review and discussion, I'm closing this. There's nothing useful that we can say about static methods and thread safety. Anything would depend on the code in the method, and where the method is called, and the context where it's called.
Activity
BillWagner commentedon Feb 12, 2019
Thanks for raising this issue @michael-hawker
There is no language level guarantee on thread synchronization for static methods. Whether or not a static method is thread safe depends on the code in the static method. If the static method does not mutate shared data, it would be thread safe. However, any access to shared data would need to be synchronized.
Because there isn't a language guarantee here, we've avoided making any statement because there isn't language guarantee about thread safety for static methods. Any given static method may be thread safe. Or, it may not be. That can only be known by understanding the implementation of the method.
michael-hawker commentedon Feb 12, 2019
Thanks @BillWagner, I understand that I can always put unsafe code somewhere that does silly things. :)
It's just helpful to know if the underlying construct is thread-safe and is only going to be able to be entered once or if I need to worry about mutexing around initializing/accessing the member or not.
BillWagner commentedon Feb 26, 2019
After some internal review and discussion, I'm closing this. There's nothing useful that we can say about static methods and thread safety. Anything would depend on the code in the method, and where the method is called, and the context where it's called.