Skip to main content

You are not logged in. Your edit will be placed in a queue until it is peer reviewed.

We welcome edits that make the post easier to understand and more valuable for readers. Because community members review edits, please try to make the post substantially better than how you found it, for example, by fixing grammar or adding additional resources and hyperlinks.

4
  • 3
    @radarbob - Minor detail: You won't lock all methods you just take a lock that more clients could be interested in. Methods are never locked, it's just that the mutex has been taken. Commented Apr 9, 2014 at 12:39
  • 3
    I suspect phrasing of this answer could be misleading - locking should not have to do anything with scope of methods - it should only be concerned with scope of shared data accessed in those methods. Instance method may not access any shared data (and hence no need for locking), may access static shared data (and hence need static lock, also refactoring may be good idea instead), same for static... Commented Dec 22, 2017 at 16:22
  • @AlexeiLevenkov: You are right that the scope should actually be decided by whether the data is static or not, but the scope of the methods should also be decided by that, so it all fits together. Instance data normally doesn't need locking, but if the instance is shared between threads then you would need locking. Commented Dec 23, 2017 at 15:34
  • Sometimes you may be using a non static method to operate to a rather global level, such as the windows registry, in that case you should use static lock to avoid any two instance to try to operate at the same time on the same registry. Commented Mar 21, 2023 at 15:29