<p>In the C programming language and Pthreads as the threading library; do variables/structures that are shared between threads need to be declared as volatile? Assuming that they might be protected by a lock or not (barriers perhaps).</p>

<p>Does the pthread POSIX standard have any say about this, is this compiler-dependent or neither?</p>

<p>Edit to add: Thanks for the great answers. But what if you're <em>not</em> using locks; what if you're using <a href="http://en.wikipedia.org/wiki/Barrier_(computer_science)" rel="noreferrer">barriers</a> for example? Or code that uses primitives such as <a href="http://en.wikipedia.org/wiki/Compare_and_swap" rel="noreferrer">compare-and-swap</a> to directly and atomically modify a shared variable...</p>

Volatile means that we have to go to memory to get or set this value. If you don't set volatile, the compiled code might store the data in a register for a long time.
What this means is that you should mark variables that you share between threads as volatile so that you don't have situations where one thread starts modifying the value but doesn't write its result before a second thread comes along and tries to read the value.
Volatile is a compiler hint that disables certain optimizations. The output assembly of the compiler might have been safe without it but you should always use it for shared values.
This is especially important if you are NOT using the expensive thread sync objects provided by your system - you might for example have a data structure where you can keep it valid with a series of atomic changes. Many stacks that do not allocate memory are examples of such data structures, because you can add a value to the stack then move the end pointer or remove a value from the stack after moving the end pointer. When implementing such a structure, volatile becomes crucial to ensure that your atomic instructions are actually atomic.