volatile

Complete example for use of volatile key word in Java?

感情迁移 提交于 2019-12-05 06:35:10
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. aioobe 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-volatile variable. Here is a program that exposes the importance of volatile in most machines I've

Should volatile and readonly be mutually exclusive?

只谈情不闲聊 提交于 2019-12-05 06:11:40
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<int>(), () => tsqueue?.Enqueue(5)); The code above is acceptably non-deterministic: the item may or may

Defining volatile class object

青春壹個敷衍的年華 提交于 2019-12-05 05:56:24
Can the volatile be used for class objects? Like: volatile Myclass className; The problem is that it doesn't compile, everywhere when some method is invoked, the error says: error C2662: 'function' : cannot convert 'this' pointer from 'volatile MyClass' to 'MyCLass &' What is the problem here and how to solve it? EDIT: class Queue { private: struct Data *data; int amount; int size; public: Queue (); ~Queue (); bool volatile push(struct Data element); bool volatile pop(struct Data *element); void volatile cleanUp(); }; ..... volatile Queue dataIn; ..... EnterCriticalSection(&CriticalSection);

Volatile guarantee safe publication of a mutable object?

笑着哭i 提交于 2019-12-05 05:01:51
By reading Java Concurrency in Practice I can see: To publish an object safely, both the reference to the object and the object's state must be made visible to other threads at the same time. A properly constructed object can be safely published by: Initializing an object reference from a static initializer Storing a reference to it into a volatile field or AtomicReference Storing a reference to it into a final field of a properly constructed object Storing a reference to it into a field that is properly guarded by a lock. However, I am confused about the second idiom. Since volatile can only

How many usage does “volatile” keyword have in C++ function, from grammar perspective?

我的梦境 提交于 2019-12-05 04:45:22
I asked this function based on this concept (maybe incorrect?!): Wherever a const can exist, a volatile can exist at the place. class classA { public: const int Foo() const; } Here the first "const" means the return value is const, we can not change it. the second const means "Is Query", this function can not change the member variable and can not call non-const function. Now comes to volatile: I can understand what volatile does on a variable, like "volatile int a;" However I have no idea of the difference among the following: Case 1: The return type is volatile? volatile void Function1();

volatile的理解

岁酱吖の 提交于 2019-12-05 03:46:48
Volatile相当于一个轻量级的锁,作用是保证变量在线程之间的可见性。 可见性指的是一个线程修改了这个变量,会被其他线程读取到。 (插一句:synchronized的锁,如果锁的是普通方法,那么锁住的就是一个实例,如果要发挥作用,那调用的需要获取的是同一把锁,也就是这个对象只能new 一个实例) 来源: https://www.cnblogs.com/1023linlin/p/11903438.html

Why Volatile is behaving weirdly

偶尔善良 提交于 2019-12-05 03:32:27
I have experience this weird behavior of volatile keyword recently. As far as i know, volatile keyword is applied on to the variable to reflect the changes done on the data of the variable by one thread onto the other thread. volatile keyword prevents caching of the data on the thread. I did a small test........ I used an integer variable named count, and used volatile keyword on it. Then made 2 different threads to increment the variable value to 10000, so the end resultant should be 20000. But thats not the case always, with volatile keyword i am getting not getting 20000 consistently, but

What is the difference between sequential consistency and atomicity?

倾然丶 夕夏残阳落幕 提交于 2019-12-05 03:21:51
I read that java volatile are sequential consistent but not atomic. For atomicity java provides different library. Can someone explain difference between two, in simple english ? (I believe the question scope includes C/C++ and hence adding those language tags to get bigger audience.) Imagine those two variables in a class: int i = 0; volatile int v = 0; And those two methods void write() { i = 5; v = 2; } void read() { if (v == 2) { System.out.println(i); } } The volatile semantics guarantee that read will either print 5 or nothing (assuming no other methods are modifying the fields of course

How is memory inconsistency different from thread interleaving?

时光毁灭记忆、已成空白 提交于 2019-12-05 02:33:04
问题 I'm writing a multi-thread program and was researching if I should use volatile for my boolean flag. The docs oracle trail on concurrency doesn't explain anything about memory consistency errors other than: Memory consistency errors occur when different threads have inconsistent views of what should be the same data. It makes sense to assume that these inconsistent views only occur after a 'write'-operation. But how long after? EXAMPLE 1 Thread A: Retrieve flag. Thread B: Retrieve flag.

Java: volatile enough to make classes threadsafe?

允我心安 提交于 2019-12-05 02:24:17
问题 I have a question about the volatile statement in Java. Please look at this constructed example: class Master { // Foo is a class with thread-safe methods public volatile Foo foo; } class Worker1 implements Runnable { protected Master master void run() { ... }; } class Worker2 implements Runnable { protected Master master void run() { ... }; } We have 2 worker threads which hold a reference to an object of class Master running at the same time. During their work, both have to access methods