-
Notifications
You must be signed in to change notification settings - Fork 5.9k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Call out thread-safety for access to static class members #10244
Comments
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. |
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. |
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.
The text was updated successfully, but these errors were encountered: