0

I have been reading several articles on threading in C# and mostly am looking for confirmation that I understand how it is working so that I do not cause unexpected behavior in the application I am developing.

So given the example below, I have a few questions.

  1. The main thread which the application is running sets the shared string to "123". It is guaranteed that the spawned off threads #2 and #3 will not return 000 when calling ConvertToNumber() because the main thread sets the string to "123" before any of those other threads are spawned. Correct?
  2. Thread #4 is guaranteed to return 789 when calling ConvertToNumber() because the string is set to "789" in at least the core's local cache and possibly in higher levels of memory such as main memory. Correct?
  3. Assume threads #2, #3, and #4 are currently running and thread #4 reaches the assignment of "789" before #2 and #3 execute ConvertToNumber(). Threads #2 and #3 then reach ConvertToNumber(). It is not guaranteed that they will return 789 because the core running those thread could have the local cached value of the string "123". Correct?

Assuming that what I stated above is indeed correct, would specifying SharedString as volatile be a valid fix? Is that the intended usage of volatile? Would also wrapping the Convert.ToInt32(SharedString) with a lock also fix the issue and if so, which is the prefered method - lock or volatile?

public static class ExampleClass
{
    public static string SharedString { get; set; } = "000";

    public static int ConvertToNumber()
    {
        return Convert.ToInt32(SharedString);
    }
}

//Main thread (thread #1)
ExampleClass.SharedString = "123";

//After setting string to "123", The following code happens in different spawned off threads running concurrently.

//thread (thread #2) is created and does the following
//...some code stuff...
ExampleClass.ConvertToNumber(); 

//thread (thread #3) is created and does the following
//...some code stuff...
ExampleClass.ConvertToNumber(); 

//thread (thread #4) is created and does the following
//...some code stuff...
ExampleClass.SharedString = "789";
ExampleClass.ConvertToNumber();
Justin
  • 6,373
  • 9
  • 46
  • 72
  • *would specifying `SharedString` as `volatile` be a valid fix?* What is broken, that you need to fix it? – user4003407 Nov 29 '17 at 16:07
  • This is not a cache coherency problem. There is no sign of any synchronization between the code in the snippet and the (invisible) code in the threads so what those threads will read is entirely unpredictable. The *volatile* keyword does absolutely nothing to change that outcome. It is the kind of code where adding the required interlocking will quickly make it pointless to use threads at all. – Hans Passant Nov 29 '17 at 16:10
  • @PetSerAl Nothing is broken. I am just stating a scenario and asking outcomes to see if I understand it correctly. – Justin Nov 29 '17 at 16:11
  • I think this will answer your questions: https://stackoverflow.com/a/17530556/5121114 – TheRock Dec 01 '17 at 15:47

0 Answers0