Skip to content
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

Closed
michael-hawker opened this issue Jan 30, 2019 — with docs.microsoft.com · 3 comments
Closed

Call out thread-safety for access to static class members #10244

michael-hawker opened this issue Jan 30, 2019 — with docs.microsoft.com · 3 comments

Comments

Copy link

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.

@BillWagner BillWagner added needs-more-info Needs more info from OP. Auto-closed after 2 weeks if no response. [org][resolution] and removed ⌚ Not Triaged Not triaged labels Feb 12, 2019
@BillWagner
Copy link
Member

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
Copy link
Author

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
Copy link
Member

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.

@BillWagner BillWagner removed the needs-more-info Needs more info from OP. Auto-closed after 2 weeks if no response. [org][resolution] label Feb 26, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants