2

None of the threads will be modifying the variable. They are all reading. Is this safe?

Lee Taylor
  • 7,761
  • 16
  • 33
  • 49
Kyle
  • 32,731
  • 39
  • 134
  • 184
  • Related: http://stackoverflow.com/questions/2076461/how-does-c-sharp-guarantees-the-atomicity-of-read-write-operations – Tim M. Mar 24 '13 at 05:10
  • 2
    Depends what type the variable is. http://blogs.msdn.com/b/ericlippert/archive/2011/05/23/read-only-and-threadsafe-are-different.aspx – ta.speot.is Mar 24 '13 at 05:11
  • @PeterRitchie Can you elaborate on what information needs to be specified to make this question answerable? – Kyle Mar 24 '13 at 05:23
  • @ta.speot.is Interesting! Thank you. – Kyle Mar 24 '13 at 05:23
  • @ta.speot.is link is dead, fortunately the web archive preserved that classic: https://web.archive.org/web/20150523082529/http://blogs.msdn.com/b/ericlippert/archive/2011/05/23/read-only-and-threadsafe-are-different.aspx – Ohad Schneider Nov 19 '22 at 13:25
  • And now on the actual Eric Lippert site: https://ericlippert.com/2011/05/23/read-only-and-threadsafe-are-different/ – Ohad Schneider Nov 20 '22 at 20:08

2 Answers2

6

It depends on what resources are being shared by your threads. Consider the following example:

public class MyClass
{
    private int i;
    public int Foo { get { return i++; } } 
}

public static class SharedResources
{
    public static const string SharedString;
    public static readonly MyClass SharedMyClass;
}

In this code sample, the reference to SharedString is thread-safe, because strings are read-only in C#. However, any thread may call SharedResources.SharedMyClass.Foo at any time, and because increments are not thread safe (unless using Interlocked.Increment), threads that read this property may get inconsistent results.

In short, if a class whose public API is read-only, but whose internals are not thread-safe, it's not safe simply have multiple threads reading from it.

p.s.w.g
  • 146,324
  • 30
  • 291
  • 331
1

In order for concurrency bugs to surface, at least one access has to be a writing/modifying one.

Be careful not to mistake getters for a variable. They might have implications not known to you when accessing.

Sebastian
  • 5,177
  • 4
  • 30
  • 47
  • 3
    Is a deadlock a concurrency bug? Deadlocks can happen with only reads if one or more of those reads triggers a static constructor that takes a lock., – Eric Lippert Mar 24 '13 at 05:23
  • 1
    OK, my answer did not take deadlocks into consideration, only race conditions. Thx for that. They sure are concurrency bugs! – Sebastian Mar 24 '13 at 05:24
  • @EricLippert really loved your "Read-only and threadsafe are different" article - is it available somewhere in your new blog? (couldn't find it so went to web.archive) – Ohad Schneider Nov 19 '22 at 13:27
  • 1
    @OhadSchneider: Thank you, I'm glad you enjoyed it. You couldn't find it because it wasn't there. It is now! https://ericlippert.com/2011/05/23/read-only-and-threadsafe-are-different/ – Eric Lippert Nov 20 '22 at 16:46
  • 1
    @EricLippert Beautiful! Added the link as a comment on the question as well :) – Ohad Schneider Nov 20 '22 at 20:09