volatile

Should my Scala actors' properties be marked @volatile?

In Scala, if I have a simple class as follows: val calc = actor { var sum = 0 loop { react { case Add(n) => sum += n case RequestSum => sender ! sum } } } Should my field sum be marked @volatile? Whilst the actor is logically single-threaded (i.e. the messages are processed sequentially), the...

why can't a local variable be volatile in C#?

public void MyTest() { bool eventFinished = false; myEventRaiser.OnEvent += delegate { doStuff(); eventFinished = true; }; myEventRaiser.RaiseEventInSeperateThread() while(!eventFinished) Thread.Sleep(1); Assert.That(stuff); } Why can't eventFinished be volatile and does it matter? It would seem to me that in this case th...

Usage of volatile specifier in C/C++/Java

While going through many resources on multithreaded programming, reference to volatile specifier usually comes up. It is clear that usage of this keyword is not a reliable way to achieve synchronization between multiple threads atleast in C/C++ and Java (versions 1.4 and earlier). Here is what wikipedia lists (without explaining how) as ...

Are volatile variable 'reads' as fast as normal reads?

I know that writing to a volatile variable flushes it from the memory of all the cpus, however I want to know if reads to a volatile variable are as fast as normal reads? Can volatile variables ever be placed in the cpu cache or is it always fetched from the main memory? ...

Volatile equivalent in VB.NET

What is the VB.NET keyword equivalent of C# "volatile"? If there is no keyword what mechanism is the equivalent? ...

Assignment to volatile variable in C#

My understanding of C# says (thanks to Jeff Richter & Jon Skeet) that assignment is "atomic". What is not is when we mix reads & writes (increment/decrement) and hence we need to use methods on the Interlocked. If have only Read & assign would both the operations be atomic? public class Xyz { private volatile int _lastValue; ...

java synchronized issue

I'm having issues with Synchronized not behaving the way i expect, i tried using volatile keyword also: Shared Object: public class ThreadValue { private String caller; private String value; public ThreadValue( String caller, String value ) { this.value = value; this.caller = caller; } public synchronized String getValue() { ...

Java Thread Shared Object Synchronization Issue

I'm having issues with Synchronized not behaving the way i expect, i tried using volatile keyword also: Shared Object: public class ThreadValue { private String caller; private String value; public ThreadValue( String caller, String value ) { this.value = value; this.caller = caller; } public synchronized String getValue() { ...

If I lock when writing to a variable, do I also need to lock when reading, if the read is otherwise atomic?

I have a class with code as follows private readonly object m_lock = new object(); private IClient m_client private object m_context; When setting the client and context, I lock as follows lock(m_lock) { m_client = theClientFromSomewhere; m_context = contextObject; } My question is, if I only need to get the m_client by it...

When to use 'volatile' or 'Thread.MemoryBarrier()' in threadsafe locking code? (C#)

When should I use volatile/Thread.MemoryBarrier() for thread safety? ...

Volatile semantic with respect to other fields

Suppose I have following code private volatile Service service; public void setService(Service service) { this.service = service; } public void doWork() { service.doWork(); } Modified field marked as volatile and it's value do not depend on it's previous value. So this is correct multithreaded code (don't bother about Service im...

Is the volatile keyword required for fields accessed via a ReentrantLock?

My question refers to whether or not the use of a ReentrantLock guarantees visibility of a field in the same respect that the synchronized keyword provides. For example, in the following class A, the field sharedData does not need to be declared volatile as the synchronized keyword is used. class A { private double sharedData; p...

Does Interlocked.CompareExchange use a memory barrier?

I'm reading Joe Duffy's post about Volatile reads and writes, and timeliness, and i'm trying to understand something about the last code sample in the post: while (Interlocked.CompareExchange(ref m_state, 1, 0) != 0) ; m_state = 0; while (Interlocked.CompareExchange(ref m_state, 1, 0) != 0) ; m_state = 0; … When the second CMPXCHG o...

does presence of mutex helps getting rid of volatile key word ?

Hi , I have a multi-R/W lock class that keeps the read, write and pending read , pending write counters. A mutex guards them from multiple threads. My question is Do we still need the counters to be declared as volatile so that the compiler won't screw it up while doing the optimization. Or does the compiler takes into account that t...

If more than one thread can access a field should it be marked as volatile?

Reading a few threads (common concurrency problems, volatile keyword, memory model) I'm confused about concurrency issues in Java. I have a lot of fields that are accessed by more than one thread. Should I go through them and mark them all as volatile? When building a class I'm not aware of whether multiple threads will access it, so s...

Volatile variable

Where is a volatile variable stored in the stored in program memory(in which section) ? ...

Is there any advantage of using volatile keyword in contrast to use the Interlocked class?

In other words, can I do something with a volatile variable that could not also be solved with a normal variable and the Interlocked class? ...

Memory barriers and large structs?

Let's say I've got a struct that consist of 100 bytes. What guarantees have I got about the following code? m_myLargeStruct = someValue; // copying 100 bytes Thread.MemoryBarrier(); // Executed by another thread, after "Thread.MemoryBarrier" was called by the first thread Console.WriteLine(m_myLargeStruct.ToString()); Does the memo...

Threading and un-safe variables

I have code listed here: Threading and Sockets. The answer to that question was to modify isListening with volatile. As I remarked, that modifier allowed me to access the variable from another thread. After reading MSDN, I realized that I was reading isListening from the following newly created thread process. So, my questions now: ...

Volatile or synchronized for primitive type?

In java, assignment is atomic if the size of the variable is less that or equal to 32 bits but is not if more than 32 bits. What(volatile/synchronized) would be more efficient to use in case of double or long assignment. like, volatile double x = y; synchronized is not applicable with primitive argument. How do i use synchronized i...