volatile

Proper use of volatile variables and synchronized blocks

[亡魂溺海] 提交于 2019-12-07 10:33:22
问题 I am trying to wrap my head around thread safety in java (or in general). I have this class (which I hope complies with the definition of a POJO) which also needs to be compatible with JPA providers: public class SomeClass { private Object timestampLock = new Object(); // are "volatile"s necessary? private volatile java.sql.Timestamp timestamp; private volatile String timestampTimeZoneName; private volatile BigDecimal someValue; public ZonedDateTime getTimestamp() { // is synchronisation

Is it useful to mark variables as volatile if they are shared across threads? [duplicate]

断了今生、忘了曾经 提交于 2019-12-07 09:55:04
问题 This question already has answers here : When to use volatile with multi threading? (5 answers) Closed 4 years ago . Notice! I'm obviously failing to make my point clearly to everyone here, and it's incredibly frustrating. My goal was to dispel the myth that volatile is effectively a no-op, that it does nothing. I was not trying to state that it should be used, that it is essential, that it is not redundant, etc. I have shown that volatile does still do a thing. I concede that it is redundant

Methods in trait become volatile methods when mixed in concrete classes in 2.9.0-1 but not 2.8.1

梦想与她 提交于 2019-12-07 09:18:10
问题 I noticed this breaking (for me using it with OGNL) change in 2.9.0-1: I find that, in 2.9, methods declared in a trait become volatile when mixed in a class: Example in 2.9.0-1 import java.lang.reflect.Modifier trait SuperTrait { def getKnoll = "Kanutten" } class KlassWithKnoll extends SuperTrait { def getKnall = "Mars" } val qsc = classOf[KlassWithKnoll] val knollGetter = qsc.getDeclaredMethod("getKnoll") println("isVolatile: " + Modifier.isVolatile(knollGetter.getModifiers())) This prints

What is the difference between volatile & extern?

扶醉桌前 提交于 2019-12-07 07:57:16
问题 Few days back i had an interview but, still I am searching for the answer. I would like to understand the significance of using volatile keyword. Find the code below: Two different scenario. //project1 //File1.c int abc;//Global variable /*And this variable is getting used in some other files too.*/ if(abc == 3) //Say { printf("abc == 3"); } else { printf("abc != 3"); } /*So if or else part will not be optimized because "abc" can not be predicted, the value can chage at any point of time */ /

mutable和volatile

Deadly 提交于 2019-12-07 07:09:08
mutable和volatile 很少遇到这两个关键字,学嵌入式估计知道后者,深入研究C++的估计知道前者。 (1)mutable 在C++中,mutable是为了突破const的限制而设置的。被mutable修饰的变量,将永远处于可变的状态,即使在一个const函数中,甚至结构体变量或者类对象为const,其mutable成员也可以被修改。 [cpp] view plain copy print ? struct ST { int a; mutable int b; }; const ST st={1,2}; st.a=11; //编译错误 st.b=22; //允许 mutable在类中只能够修饰 非静态数据成员 。mutable 数据成员的使用看上去像是骗术,因为它能够使const函数修改对象的数据成员。然而,明智地使用 mutable 关键字可以提高代码质量,因为它能够让你向用户隐藏实现细节,而无须使用不确定的东西。我们知道,如果类的成员函数不会改变对象的状态,那么这个成员函数一般会声明成const的。但是,有些时候,我们需要在const的函数里面修改一些跟类状态无关的数据成员,那么这个数据成员就应该被mutalbe来修饰。 [cpp] view plain copy print ? class ST { int a; mutable int showCount; void

Is volatile required here

≡放荡痞女 提交于 2019-12-07 07:04:56
问题 I'm implementing a 'sequence lock' class to allow locked write and lock-free reads of a data structure. The struct that will contain the data contains the sequence value, which will be incremented twice while the write takes place. Once before the writing starts, and once after the writing is completed. The writer is on other threads than the reader(s). This is what the struct that holds a copy of the data, and the sequence value looks like: template<typename T> struct seq_data_t { seq_data_t

Argument of type “volatile char *” is incompatible with parameter of type “const char *”

偶尔善良 提交于 2019-12-07 05:53:52
问题 I have a function whose prototype is as follows: void foo(const char * data); Elsewhere in my code, I have a global variable declared as follows volatile char var[100]; Whenever I try to do this: foo(var); The compiler throws up the following error message: Argument of type "volatile char *" is incompatible with parameter of type "const char *" Why is that the case? As I understand it, the variable in my function is not allowed to change the pointer or its contents. I understand that because

different thread accessing MemoryStream

笑着哭i 提交于 2019-12-07 04:45:21
问题 There's a bit of code which writes data to a MemoryStream object directly into it's data buffer by calling GetBuffer(). It also uses and updates the Position and SetLength() properties appropriately. This code works properly 99.9999% of the time. Literally. Only every so many 100,000's of iterations it will barf. The specific problem is that the Position property of MemoryStream suddenly returns zero instead of the appropriate value. However, code was added that checks for the 0 and throws an

Complete example for use of volatile key word in Java?

落爺英雄遲暮 提交于 2019-12-07 03:56:03
问题 I need a simple example of use of the volatile keyword in Java, behaving inconsistently as a result of not using volatile . The theory part of volatile usage is already clear to me. 回答1: First of all, there's no guaranteed way of exposing caching due to non-volatile variables. Your JVM might just be very kind to you all the time and effectively treat every variable as volatile. That being said, there are a few ways to increase probability of having threads caching their own versions of a non

Should volatile and readonly be mutually exclusive?

岁酱吖の 提交于 2019-12-07 02:22:35
问题 Suppose that I were designing a thread-safe class that wraps an internal collection: public class ThreadSafeQueue<T> { private readonly Queue<T> _queue = new Queue<T>(); public void Enqueue(T item) { lock (_queue) { _queue.Enqueue(item); } } // ... } Based on my other question, the above implementation is buggy, as race hazards may arise when its initialization is performed concurrently with its usage: ThreadSafeQueue<int> tsqueue = null; Parallel.Invoke( () => tsqueue = new ThreadSafeQueue