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...
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...
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 ...
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?
...
What is the VB.NET keyword equivalent of C# "volatile"?
If there is no keyword what mechanism is the equivalent?
...
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;
...
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() {
...
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() {
...
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 should I use volatile/Thread.MemoryBarrier() for thread safety?
...
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...
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...
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...
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...
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...
Where is a volatile variable stored in the stored in program memory(in which section) ?
...
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?
...
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...
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:
...
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...