-4

I was explaining why volatile keyword is necessary to accessing a shared memory structure by different threads.

My argument is suppose that CPU have two cores and two local caches inside them. Suppose that one thread is running in one core and other thread is running in next core. Then when 1st thread is writing that memory , and 2nd thread is reading it, the up-to date version would not be visible to the 2nd thread due to it's local cache yet not updated.

But my friend come up with an argument, he argue what's happen before the 'volatile' keyword , the old multi-threaded application binaries brought and run in a modern processor, will they fail ? For a example a old 32-bit application brought back and run in multi-core CPU in windows 7? A application that written in era where CPU cores were not introduced so only single cache is there?

Is my friends argument is correct. Or can it fail ? What is the time-frame that 'volatile' keyword was introduced to C/C++?

sandun dhammika
  • 881
  • 3
  • 12
  • 31
  • Reason for voting to close ? – sandun dhammika Dec 23 '17 at 04:38
  • Possible duplicate of [When to use volatile with multi threading?](https://stackoverflow.com/questions/4557979/when-to-use-volatile-with-multi-threading) – Borgleader Dec 23 '17 at 04:44
  • 2
    I regret to inform you that both your friend's argument, and your argument, are [not even wrong](https://rationalwiki.org/wiki/Not_even_wrong). To even begin to unravel all the mistaken assumptions embedded in both positions would require an entire book. [This book might be a good place to start](https://www.manning.com/books/c-plus-plus-concurrency-in-action). (Note: I have not actually read that book, it was just the first thing that came up when I threw "c++ concurrency" at a search engine.) – zwol Dec 23 '17 at 04:45

2 Answers2

1

For very tight loops a mutex might not be enough. I have seen code where the loop was such that the compiler never re-loaded the test variable, with or without a mutex (and yes, I now know that testing a mutex in such a loop is poor design anyway). If a mutex was present in that example it would dump the current register value to the stack and re-load from that after the mutex was acquired rather than re-loading from the original variable's location. In that case using volatile forced the re-load from the correct address. Without a mutex the loop was tight enough there wasn't any register spill and so without volatile there wasn't any re-load at all.

SoronelHaetir
  • 14,104
  • 1
  • 12
  • 23
  • volatile is still ambiguous to me. Somewhere it tells that it just turn off the compiler optimization , for a example it always force the variable to read from memory, but does not guarantee it will read from the down main memory. There are instructions to invoke a memory read from main memory, for a example when DMA was completed, in that situation without touching assembly what keyword should I use in C source level ? – sandun dhammika Dec 23 '17 at 08:04
  • @sandundhammika For that, you're probably looking for C11's atomic types. – zwol Dec 23 '17 at 17:08
  • what if we are not speaking about C11 ? what for old versions of C++ but not just C. ? – sandun dhammika Dec 26 '17 at 10:00
  • @sandundhammika Prior to C11, some compilers had extensions equivalent to the C11 atomic types, but there's no universal solution. – zwol Dec 27 '17 at 01:18
-1

As for your thinking in your first paragraph. Any program that uses multiple threads will use a mutex to only allow one thread to change a piece of memory at a time.

The volatile keyword is usually not needed. It is used when something outside of the control of the program you are compiling might be messing with stuff you have in memory.

If you run an old application on your computer, it will only use one processor, so you're never going to run into any issues.

When used, the volatile keyword tells the compiler to be careful about how it optimizes the code. Not optimizing certain parts of the code is not going to break anything.

hostingutilities.com
  • 8,894
  • 3
  • 41
  • 51
  • This answer, like the question, is not even wrong, and on top of that repeats common misconceptions about `volatile`. – zwol Dec 23 '17 at 05:25