FAQ Database Discussion Community


Memory visibility guarantees provided by intrinsic locking in Java?

java,multithreading,synchronization,locking,volatile
I need some clarity on what memory visibility guarantees are provided by using an intrinsic lock in Java. So for example say if I have a HashMap object that maps Strings to Person objects like so: HashMap<String,Person> m = new HashMap<String, Person>(); And lets say we have a synchronized method...

Volatile for structs and collections of structs

c#,.net,multithreading,volatile,concurrentdictionary
I would like to use net wisdom to clarify some moments regarding multi-threading in .net. There are a lot of stuff in the internet about it however I was not able to find a good answer to my question. Let say we want to maintain a state of something in...

Volatile specifier ignored in C++

c++,templates,c++11,volatile
I'm pretty new to C++ and recently I ran across some info on what it means for a variable to be volatile.As far as I understood, it means a read or write to the variable can never be optimized out of existence. However a weird situation arises when I declare...

Can operations, which assign values to multiple volatile fields consecutively, be reordered?

java,concurrency,thread-safety,volatile,java-memory-model
Will the following code always print-out the "correct" value of data? Even if there are other operations between assigning the values to data and dataReady in method setData? Or could the JVM conceivably re-order these operations such that RunningThread could see dataReady as true whilst data were still null? If...

Will volatile fix this Multi-Thread Code?

java,multithreading,volatile
I have two threads updating a shared int at the same time. Most of times, this code will print 0 and 1. But some times, it will print 0 and 0 (one update failed). If I make the int value volatile, will fix that update fail, once for all? This...

Why do thread behave different with different run method body?

java,multithreading,synchronization,volatile
This code is from Effective Java (Item 66): (without sync or volatile this never ends) public class ThreadPractice { static boolean canrunstatic; public static void main(String[] args) throws InterruptedException { Thread backgroundThread = new Thread(new Runnable() { public void run() { int i = 0; while (!canrunstatic){i++;} System.out.println("finished"); } });...

Is processor cache flushed during context switch in multicore?

caching,multicore,volatile,flush,context-switch
Recently, I discussed why there is a volatile mark at seq in Java Actors demo @volatile private var seq = 0L private def nextSeq: Long = { val next = seq seq += 1 next } One answer was that threads can be migrated and variables lost (other cores will...

effects of several writes on a volatile variable in Java

java,multithreading,concurrency,volatile
I have found in this site some interesting questions (e.g., this one) about the visibility effects of volatile variables in Java originated from this paragraph taken from the book Java concurrency in Practice: The visibility effects of volatile variables extend beyond the value of the volatile variable itself. When thread...

Should I use 'Volatile' in following case?

c,volatile
Consider that I have multiple '.c' source files in my application. Module1.c static char status_variable = 0; Modify1() { status_variable = 'a'; } Modify2() { status_variable = 'x'; } char GetStatus() { return status_variable; } The function Modify can be called from other .c files based on events(Not through ISR's)...

volatile keyword and RAII idiom (C++)

c++,volatile,raii
assuming to have a class to control concurrent access to a (critical) code block similar to this one: class RAIIObj : public boost::noncopyable { public: explicit RAIIObj( LockObj& Obj ) : LkObj( Obj ) { Obj.Acquire(); } ~RAIIObj() { try { Obj.Release(); } catch (...) {} } private: LockObj& LkObj;...

Volatile keyword does not work as expected with multiple instances of a class [duplicate]

java,multithreading,volatile
This question already has an answer here: Why volatile is not working properly 6 answers I have read in almost all the posts that volatile (even if it's not static) variable is shared among the the threads. When one thread updates the variable then second thread gets the updated...

Force JVM to cache variables for threads

jvm,volatile,jvm-arguments,memory-barriers
Is there a way to tell the JVM to cache variables for threads as long as possible and never update them unless memory barriers or volatile variables are used? (for testing)

Am I right volatile keyword in C needs special hardware support to work?

c++,c,caching,volatile
I understand what volatile does and what it doesn't do, taking the example from this question void waitForSemaphore() { volatile uint16_t* semPtr = WELL_KNOWN_SEM_ADDR;/*well known address to my semaphore*/ while ((*semPtr) != IS_OK_FOR_ME_TO_PROCEED); } my question is: in the presence of cpu cache, volatile can not guarantee the above works,...

What happen when I run MemoryBarrier() exactly ? and how do it?

c#,.net,vb.net,thread-safety,volatile
According to Web, I found the following codes, which is equivalent of C# Volatile for VB.NET. Code reference: How do I specify the equivalent of volatile in VB.net? Function VolatileRead(Of T)(ByRef Address As T) As T VolatileRead = Address Threading.Thread.MemoryBarrier() '*** I mean this line ***' End Function Sub VolatileWrite(Of...

Connecting R To Teradata VOLATILE TABLE

r,teradata,volatile,rodbc
I am using R to try and connect to a teradata database and am running into difficulties The steps in the process are below 1) Create Connection 2) Create a VOLATILE TABLE 3) Load information from a data frame into the Volatile table Here is where it fails, giving me...

Why is the planner coming up with different results for functions with different volatilities?

postgresql,function,volatile,query-plans
This question comes as a follow up to and a result of SQL function very slow compared to query without function wrapper. I should note that I don't consider this a duplicate, since that question was asking for a solution to a specific problem. I am asking for more information...

Volatile keyword in microcontrollers [closed]

c++,c,microcontroller,volatile
The volatile keyword tells the compiler not to optimize the variable which is prefixed. The variable may change during run time by unknown source (not known by the compiler) maybe by an external interrupt etc. Is there any other advantage of volatile? Does volatile apply to reading from files?...

C# not thread-safe initialization

c#,multithreading,volatile
There are two threads A and B. Thread A performs the following assignment: v = new Vec2() Thread B uses v, for example: double length = Math.Sqrt(v.x*v.x + v.y*v.y); Console.WriteLine("length is {0}", length); v is a non-volatile field, Vec2 is a reference type with some initialization: class Vec2 { public...

CancellationTokenSource vs. volatile boolean

c#,task,volatile,cancellationtokensource
Are there any benefits for using a CancellationTokenSource over a volatile boolean field for signalling a Task to finish?

C# variable freshness

c#,multithreading,task-parallel-library,volatile,memory-model
Suppose I have a member variable in a class (with atomic read/write data type): bool m_Done = false; And later I create a task to set it to true: Task.Run(() => m_Done = true); I don't care when exactly m_Done will be set to true. My question is do I...

Java memory model: volatile variables and happens-before

java,volatile,thread-synchronization,java-memory-model,happens-before
I'd like to clarify how happens-before relation works with volatile variables. Let we have the following variables: public static int i, iDst, vDst; public static volatile int v; and thread A: i = 1; v = 2; and thread B: vDst = v; iDst = i; Are the following statements...

java do interruptions act volatile?

java,multithreading,concurrency,interrupt,volatile
If one thread interrupts another, will the interrupted status be immediately visible (i.e can it have visibility problems)? On top of that I am wondering do you ever use interruptions? A Volatile boolean flag seems way more reliable...

Can an uninitialized automatic volatile variable in C be safely read?

c,volatile,undefined-behavior
In C, can I access an automatic volatile variable without having to initialize it first, or does it always result in undefined behavior? E.g., in some hardware devices mapped to volatile variables, initializing the variable makes no sense anyway, or might even be forbidden....

Using volatile variables and semaphores - Java

java,multithreading,mutex,semaphore,volatile
I'm starting with Threads, Semaphores, volatile variables, etc. I wonder if when I'm using Semaphores it is necessary to define the variable as volatile, I mean: Having 2 Threads, one increases and the other decreases the variable for example and obviously, before each access I have a mutex that controls...