volatile

How do I Understand Read Memory Barriers and Volatile

Some languages provide a volatile modifier that is described as performing a "read memory barrier" prior to reading the memory that backs a variable. A read memory barrier is commonly described as a way to ensure that the CPU has performed the reads requested before the barrier before it performs a read requested after the barrier. Howe...

C# volatile array items?

Hi I need an array with volatile items, and can't find a way to do that. private volatile T[] _arr; This means that the _arr reference is volatile, however it does not guarantee anything about the items inside the _arr object itself. Is there any way to mark the _arr's Items as volatile? Thanks. EDIT: The following code built acc...

volatile boolean

If I have a volatile boolean (let's call it valid), is the following piece of code thread-safe in Java? if (valid) return; valid = true; Or, do I need to synchronize since valid is set to true only if it's false (hence set of valid depends on its current value)? ...

Concurrency, object visibility

I'm trying to figure out if the code below suffers from any potential concurrency issues. Specifically, the issue of visibility related to volatile variables. Volatile is defined as: The value of this variable will never be cached thread-locally: all reads and writes will go straight to "main memory" public static void main(String [] ar...

Determinate when an object not used by any other threads without lock?

Hi I implementing a high performance thread safe component, using no lock statements, only volatile and Interlocked are used for performance reasons. I have volatile reference-type member in a class, that contains thread safe instance. This instance is thread safe only for a couple of operations, and not for another. Because of that an...

The need for volatile modifier in double checked locking in .NET

Multiple texts say that when implementing double-checked locking in .NET the field you are locking on should have volatile modifier applied. But why exactly? Considering the following example: public sealed class Singleton { private static volatile Singleton instance; private static object syncRoot = new Object(); private Sing...

Volatile Struct Semantics

Is it sufficient to declare an instance of a structure-typed variable as volatile (if its fields are accessed in re-entrant code), or must one declare specific fields of the structure as volatile? Phrased differently, what are the semantic differences (if any) between: typdef struct { uint8_t bar; } foo_t; volatile foo_t foo_inst; ...

.NET memory model, volatile variables, and test-and-set: what is guaranteed?

I know that the .NET memory model (on the .NET Framework; not compact/micro/silverlight/mono/xna/what-have-you) guaranteed that for certain types (most notably primitive integers and references) operations were guaranteed to be atomic. Further, I believe that the x86/x64 test-and-set instruction (and Interlocked.CompareExchange) actuall...

lockless threading question

I've been reading Joe Duffy's book on Concurrent programming. I have kind of an academic question about lockless threading. First: I know that lockless threading is fraught with peril (if you don't believe me, read the sections in the book about memory model) Nevertheless, I have a question: suppose I have an class with an int proper...

How to declare array elements volatile in Java?

Is there a way to declare array elements volatile in Java? I.e. volatile int[] a = new int[10]; declares the array reference volatile, but the array elements (e.g. a[1]) are still not volatile. So I'm looking for something like volatile int[] a = new volatile int[10]; but it doesn't work that way. Is it possible at all? ...

Why/when are session writes vulnerable to thread termination?

THE CODE: Session["foo"] = "bar"; Response.Redirect("foo.aspx"); THE PROBLEM: When foo.aspx reads "foo" from the session, it's not there. The session is there, but there's no value for "foo". I've observed this intermittently in our production environment. But I don't mean here to ask a question about Response.Redirect(). THE E...

Example of 'volatile' preventing a compiler optimization in C#?

From what I understand, the 'volatile' modifier in C# has two effects: Inserts fences as necessary for the target processor Prevents certain compiler optimizations On x86 / amd64, (1) is irrelevant. Those processors don't require fences for volatile semantics. (ia64 is different, though.) So, we are down to (2). But, for examples th...

How do I declare an array created using malloc to be volatile in c++

I presume that the following will give me 10 volatile ints volatile int foo[10]; However, I don't think the following will do the same thing. volatile int* foo; foo = malloc(sizeof(int)*10); Please correct me if I am wrong about this and how I can have a volatile array of items using malloc. Thanks. ...

c++ volatile multithreading variables

I'm writing a C++ app. I have a class variable that more than one thread is writing to. In C++, anything that can be modified without the compiler "realizing" that it's being changed needs to be marked volatile right? So if my code is multi threaded, and one thread may write to a var while another reads from it, do I need to mark the v...

Volatile Vs Static in java

Is it like, static means one copy of the value for all objects , volatile means one copy of the value for all thread.? any way static variable value also going to be one value for all threads, then why should we go for volatile. Please help. I have confusion on this. ...

pthread_exit and/or pthread_join causing Abort and SegFaults.

The following code is a simple thread game, that switches between threads causing the timer to decrease. It works fine for 3 threads, causes and Abort(core dumped) for 4 threads, and causes a seg fault for 5 or more threads. Anyone have any idea why this might be happening? #include <stdio.h> #include <stdlib.h> #include <pthread.h> ...

Does Monitor.Wait ensure that fields are re-read?

It is generally accepted (I believe!) that a lock will force any values from fields to be reloaded (essentially acting as a memory-barrier or fence - my terminology in this area gets a bit loose, I'm afraid), with the consequence that fields that are only ever accessed inside a lock do not themselves need to be volatile. (If I'm wrong a...

C++ language some live examples for mutable...

Respected sir , I need some help for mutable keyword it is used in a const function and please any body explain for the live example about the mutable and constant function and also diff. for the volatile member and function please help me in Advance Thank you, ...

Java: volatile guarantees and out-of-order execution

IMPORTANT EDIT I know about the "happens before" in the thread where the two assignments are happening my question is would it be possible for another thread to be reading "b" non-null while "a" is still null. So I know that if you're calling doIt() from the same thread as the one where you previously called setBothNonNull(...) then it c...

What is the purpose of a volatile member function in C++?

What is the purpose of a volatile member function in C++? ...