-1

I am reading this stackoverflow post, which give the sample code as :

static void modifySharedResource(SomeClass sc)
    {
        //do something
        lock (_lock)
        {
            //where sc is modified
        }
    }

I am curious that, why this static method need a lock, does the static method thread-safe in this sample ?

I also read this post, but I didn't understand the answer.

Could anyone give more detailed information about my question, thanks !

Community
  • 1
  • 1
allencharp
  • 1,101
  • 3
  • 14
  • 31
  • looks like a singleton? – user1666620 Aug 25 '15 at 14:15
  • 4
    whether a method is thread safe or not depends on whether or not it accesses shared resources (e.x static variables,..). There is nothing to do with whether the method is static – Khanh TO Aug 25 '15 at 14:16
  • some other method may be currently acting on the object `sc`. Using the lock in both places ensures those operations don't occur at the same time. – Jonesopolis Aug 25 '15 at 14:17
  • @Jonesopolis Generally it would be the responsibility of the caller of this method to ensure that the provided parameter isn't being manipulated in another thread while this thread is running. – Servy Aug 25 '15 at 14:38

3 Answers3

2

Whether a method is thread safe or not depends on whether or not it accesses shared resources (e.x static variables,..). There is nothing to do with whether the method is static.

In your case, it's not sure to tell whether modifying sc is thread safe or not. It depends on how this parameter is created and passed in to the method. In most of the cases, it's problematic if the passed in parameter is shared. When we develop a function, we should ensure that there is no problem no matter how the function is used.

With your lock (I assume that _lock is a static variable), you still don't achieve thread safe if sc is shared and modified somewhere else on another thread.

With the method named modifySharedResource (the parameter is shared), I'm quite sure there is a problem if the parameter is also modified somewhere else on another thread.

In general, it's a bad practice to modify incoming parameters: https://softwareengineering.stackexchange.com/questions/245767/is-it-an-antipattern-modifying-an-incoming-parameter

Community
  • 1
  • 1
Khanh TO
  • 48,509
  • 13
  • 99
  • 115
0

you have a misunderstanding with thread-safe, keyword lock marks bit in sync block index of own parameter (_lock in your code) and another lock can't pass until other lock release _lock and revert bit

burning_LEGION
  • 13,246
  • 8
  • 40
  • 52
-1

The issue here is that the SomeClass is not thread safe. The static method as you have it here is thread safe.

CodeTherapist
  • 2,776
  • 14
  • 24
  • I am not sure how you concluded that the method with // do stuff is thread-safe. – SergeyA Aug 25 '15 at 17:39
  • As I said: `the method as you have it here` is thread safe. Unfortunately I can not give any advice for code that is not in the example! So `//do something` could be or could not be thread safe. – CodeTherapist Aug 26 '15 at 07:09