-4

Take the following examples:

List<MyObject> objects = new List<MyObject();

Example #1:

static class MyClass1
{
    static void MyMember(List<MyObject> objects) { objects.Add(new MyObject); }
}

Example #2:

class MyClass2
{
    void MyMember(List<MyObject> objects) { objects.Add(new MyObject); }
}

Example #3:

static MyClass3 myClass3 = new MyClass3();
class MyClass3
{
    void MyMember(List<MyObject> objects) { objects.Add(new MyObject); }
}

and then calling

 Task.Factory.StartNew(() =>
 {
    MyClass1.MyMember(objects);
 });

or

 MyClass2 myClass2 = new MyClass2();
 Task.Factory.StartNew(() =>
 {
    myClass2.MyMember(objects);
 });

or

 static MyClass3 myClass3 = new MyClass3();
 Task.Factory.StartNew(() =>
 {
    myClass3.MyMember(objects);
 });

Assuming that the application will only have one of these examples, and that the Task is called multiple times simultaneously.

You could think of this as a console app where List<MyObject> objects is instantiated in the Main(), same goes for the tasks, called in Main() multiple times simultaneously.

Brad Larson
  • 170,088
  • 45
  • 397
  • 571
Reousa Asteron
  • 519
  • 1
  • 3
  • 18
  • It's not really clear what you're asking. Please review http://stackoverflow.com/questions/261683/what-is-meant-by-thread-safe-code – Alex Paven Mar 31 '17 at 17:52
  • I'll try to clarify further – Reousa Asteron Mar 31 '17 at 17:53
  • Everything is pretty much tread safe unless you have a point in your application where multiple threads can collide. For example, reading from same list is safe but changing it is not thread safe, hence need for using lock. – T.S. Mar 31 '17 at 17:53
  • 1
    @T.S. Almost nothing in any program is thread safe unless you go through *extreme* pains to make it safe, which most people don't do, because they don't have any reason to (because they aren't calling that code from multiple threads). – Servy Mar 31 '17 at 17:57
  • I have clarified the question a bit more, I hope it makes more sense now! – Reousa Asteron Mar 31 '17 at 17:58
  • Your example starts a single task so it still doesn't 'need' any thread safety. Perhaps a better example would be three tasks that all call the same method on the same object. But even then it simply depends on what the method does. If it doesn't do anything that can affect the execution of the other threads (like return a constant), it's thread safe. So it doesn't really matter how the class or method is defined, what matters is what it does. – Alex Paven Mar 31 '17 at 18:01
  • @Servy you are certainly correct to look at it this way because this will make you think, "is this thread safe?". However, usually in most applications there are only certain points where you need to take care of thread safety concerns. I don't remember writing locks every day. – T.S. Mar 31 '17 at 18:02
  • @AlexPaven The example involves multiple threads; the thread that's starting the new task, and the new task that is started. There are any number of possible things that could be done in the code not shown that would break the code. – Servy Mar 31 '17 at 18:02
  • 1
    @T.S. Reading from a list isn't thread safe. If the list changes during the iteration an error occurs. Most applications run on a single thread, hence the lack of threading code. Even async calls won't guarantee multithreadedness. – Cameron MacFarland Mar 31 '17 at 18:04
  • @T.S. Indeed, you don't need to care at all about thread safety in most programs that most people write. The fact that it's *perfectly okay* that most code most people write isn't thread safe (because it isn't sharing state between threads) is very different from saying that all of that code is thread safe, because it's not. Saying that all of that code is thread safe when it's not would lead people to believe they can use that code in a multithreaded situation, and they simply can't. – Servy Mar 31 '17 at 18:05
  • @CameronMacFarland please inspect what I said carefully. I said that no lock is needed unless list can be changed by another thread – T.S. Mar 31 '17 at 18:05
  • @ReousaAsteron They're all safe, if the `MyMember` methods are empty. The problem occurs when that code tries to work with shared data, like a common object. – Cameron MacFarland Mar 31 '17 at 18:06
  • @T.S. No, that's *not* all that you said, "Everything is pretty much tread safe [...]" That's simply false. That code *isn't* thread safe. It's simply not a problem that it's not thread safe. – Servy Mar 31 '17 at 18:07
  • Thanks for the very informative comments, I am gaining quite a bit of knowledge just reading this. I have updated the question to make more sense (again), sorry if it didn't earlier, I was only trying to make it brief. – Reousa Asteron Mar 31 '17 at 18:08
  • @Servy I am not going to discuss commonly thread-unsafe code that deemed safe by developers because the way they use it. This generalizes your opinion about any given developer. – T.S. Mar 31 '17 at 18:13
  • Is `List objects;` supposed to be a member of MyClass (you cant declare a variable outside the scope of a class in C#)? If MyClass is an inner class defined within the same class definition as `objects` then you would need an explicit reference to it (they're not implicit like in Java). – samus Mar 31 '17 at 18:24
  • @SamusArin I have updated the question again. – Reousa Asteron Mar 31 '17 at 18:34
  • I've updated the question further, hopefully what I'm asking is clearer now. – Reousa Asteron Mar 31 '17 at 18:54
  • 3
    @AlexPaven Pretty clear what OP is asking to me, asking us to do their homework. – Andrew Mar 31 '17 at 19:32
  • @Andrew Heh I was thinking same thing. – samus Mar 31 '17 at 19:40
  • @Andrew No I am not, as a matter of fact I am trying to understand how the static keyword would affect certain aspects of my code. I don't get why people bash on noobs all the time. I've changed the question title once more, should really start being more forgiving, unless you've started programming on an expert level since day 1, then do what you want and accept my appologies for not being on the same level as you are sensei. – Reousa Asteron Apr 01 '17 at 07:35
  • @ReousaAsteron The structure of your question is the kind of thing one would see in a programming class on a test or in homework. Listing numbered options like you did and even more so with the words "Assuming that the application will only have one of these examples" very homework question. – Andrew Apr 01 '17 at 12:42
  • @Andrew I wouldn't need to come to stack overflow if it was homework though would I? I could've simply asked a colleague to help with it. Just because you think it looks like HW simply doesn't mean it is. Then again, I'm not here to argue, I'm here to learn. I will certainly take your argument into consideration the next time I post a question though. – Reousa Asteron Apr 01 '17 at 12:59
  • Dear all, I have revisited this question in an attempt to remedy old mistakes. Thank you all for the criticism, hopefully I have learned a thing or two! :) – Reousa Asteron Sep 25 '18 at 08:12

2 Answers2

1

It depends on internal logic of methods. Currently (with empty implementations) they are all thread-safe.

UPD:

And now they are all NOT thread-safe. I'd like to note that static keyword doesn't provide any "magic" on its own. May be you are confused due to common convention in .NET base class library to make all static members thread-safe and all instance members not thread-safe. While this is true, but it is only convention which developers of BCL decided to follow. In your own code static doesn't provide any thread-safety until you implement it by yourself.

1

For Example #1 to work, you would have to have

static class MyClass1
{
    static List<MyObject> objects = new List<MyObject();
    static void MyMember() { objects.Add(new MyObject); }
}

in order for MyMember to access objects. Since there is only ever a single instance of a static class'es data members (which must all be static since a static class cannot be constructed (it's not dynamically allocated on the heap like a reference variable, but instead allocated in a separate, "global", memory area) and therefore have instance variables), this would not be thread safe.

samus
  • 6,102
  • 6
  • 31
  • 69