Java volatile in C?

I know about using volatile in Java. This (based on wikipedia article ):

There is a global order of reading and writing to a mutable variable. This means that every thread accessing an unstable field will read its current value before continuing, rather than (potentially) using a cached value.

I also know that there is a volatile keyword in C, but in a completely different context, mainly for use in memory I / O.

So, I was wondering if there is any construct like Java volatile in C? Will this prevent reading cached variable values?

If it does not exist in C, is there possibly a library with a construct like pthread ?

+4
source share
2 answers

volatile in C is basically an anachronism designed for a completely different scenario and NOT useful for multi-threaded programming. For one reason or another, even the latest C ++ standard could not give it more meaning, but had to come up with a new keyword.

Remember that this means that volatile , as defined by the C standard, is useless, compilers can give you additional guarantees (I know what MS VC does, and I think it gives basically the same guarantees as volatile in java) , but that means the program is blocked by this compiler. Most compilers also have some options for inserting memory barriers, which are slightly more explicit, but again not portable as such.

In practice, you are probably best using a higher-level thread library that offers the right tools for the job. For instance. POSIX gives you low-level afaik memory barriers.

On a C ++ site, better with 0x11 - the standard offers std::atomic_thread_fence and atomic variables. Note that the C ++ 0x11 memory model is NOT identical to java, so you need to be careful when porting.

+5
source

The behavior of volatile not necessarily different; it is a platform and context that is different. You specifically mentioned memory mapped devices in your question. If you have a memory-mapped device, such as a piece of hardware, that can flip the register that is represented by a variable in your code, then obviously you need a way to specify this. If you do not, then the compiler will always work under the assumption that the only system that can modify your code is your program and can optimize it.

A good example would be if you use a variable in a decision or flow control situation. If this variable is never controlled directly in your code, but can be turned over by a signal or some hardware with memory mapping, the compiler can optimize the acceptance condition in a boolean value, since it will assume that the value will not change. Therefore, when the hardware reverses the value, it is not reflected in the current code due to compiler optimization.

Java caches have basically the same behavior. Your disconnect seems to come from the fact that you are not creating a mental bridge between C, which is compiled code, and Java is JIT-compiled byte code running inside a virtual machine. Java inherits volatile behavior from C, but the consequences of this behavior are very different due to the context of the runtime.

+3
source

Source: https://habr.com/ru/post/1395815/


All Articles