volatile

what happens after writing to a volatile variable?

余生颓废 提交于 2019-12-08 16:14:27
问题 I wonder if writing to a volatile variable will force jvm to synchronize all non-volatile variables to the memory, so for example, what will happen in the following code: volatile int x; int y; y=5; x=10; x will be written to the memory, but what will happen to y ? will it be also written to memory ? 回答1: Yes, under the rules of the Java Language Specification (third edition) -- in particular section 17.4.4 -- every thread that sees the new value of x will subsequently also see the new value

synchronized counter in clojure

两盒软妹~` 提交于 2019-12-08 15:57:58
问题 If I want to keep a global counter (e.g. to count number of incoming requests across multiple threads), then the best way to do in java would be to use a volatile int. Assuming, clojure is being used is there a better (better throughput) way to do? 回答1: I would do this with an atom in Clojure: (def counter (atom 0N)) ;; increment the counter (swap! counter inc) ;; read the counter @counter => 1 This is totally thread-safe, and surprisingly high performance. Also, since it uses Clojure's

Do we really need VOLATILE keyword in C#?

不打扰是莪最后的温柔 提交于 2019-12-08 15:40:17
问题 Here is the code that I was trying on my workstation. class Program { public static volatile bool status = true; public static void Main() { Thread FirstStart = new Thread(threadrun); FirstStart.Start(); Thread.Sleep(200); Thread thirdstart = new Thread(threadrun2); thirdstart.Start(); Console.ReadLine(); } static void threadrun() { while (status) { Console.WriteLine("Waiting.."); } } static void threadrun2() { status = false; Console.WriteLine("the bool value is now made FALSE"); } } As you

Why doesn't C# volatile protect write-read reordering?

强颜欢笑 提交于 2019-12-08 14:40:07
问题 According to this online book, the volatile keyword in C# does not protect against reordering Write operations followed by Read operations. It gives this example in which both a and b can end up being set to 0 , despite x and y being volatile : class IfYouThinkYouUnderstandVolatile { volatile int x, y; void Test1() // Executed on one thread { x = 1; // Volatile write (release-fence) int a = y; // Volatile read (acquire-fence) ... } void Test2() // Executed on another thread { y = 1; //

Connecting R To Teradata VOLATILE TABLE

落爺英雄遲暮 提交于 2019-12-08 10:02:57
问题 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 an error message Error in sqlSave(conn, mydata, tablename = "TEMP", rownames = FALSE, : first argument is not an open RODBC channel The code is below # Import Data From Text File and remove duplicates mydata = read.table("Keys.txt

Volatile Pointer to Non Volatile Data

冷暖自知 提交于 2019-12-08 03:31:03
问题 Suppose I have the following declaration: int* volatile x; I believe that this defines a volatile pointer "normal" variable. To me this could mean one of two things: First Guess The pointer can change, but the number will not change without notice. This means that some other thread (that the compiler doesn't know about) can change the pointer, but if the old pointer was pointing to a "12" then the new pointer (the new value of the pointer, because the thread changes it) would point to another

Volatile C/C++ on a multithread app

雨燕双飞 提交于 2019-12-08 02:20:21
问题 I am having a question on the volatile usage. I typically try that all the variables shared across threads have the volatile keyword to ensure direct memory accesses, and of course protected with mutexes. However, is volatile really needed if a variable is shared to ensure consistency? I explain with an example: Thread1: //(affects it behaviour with the variable) mymutex.lock(); if(variable) { ... variable = false; } mymutex.unlock(); Thread2: mymutex.lock(); variable = true; mymutex.unlock()

Causing non-atomics to tear

心不动则不痛 提交于 2019-12-08 01:19:46
问题 Hi I would like a int and a float example that causes tearing for writing with an non-atomic values. I can't seem to reproduce this. It seems like something that is extremely rare or something I'm doing wrong. Here is my test code which never prints. Is there anything wrong with it? #include <windows.h> #include <tchar.h> #include <strsafe.h> #define MAX_THREADS 64 #define BUF_SIZE 255 DWORD WINAPI MyThreadFunction( LPVOID lpParam ); void ErrorHandler(LPTSTR lpszFunction); // Sample custom

Do I need to use volatile keyword for memory access in critical section?

若如初见. 提交于 2019-12-07 14:21:20
问题 I am writing code for a single processor 32 bit microcontroller using gcc. I need to consume time-stamped objects from a linked list. Another part of the code which could be asynchronous (maybe in an ISR) adds them to the list. The critical section is implemented by turning interrupts off and using the barrier() function. I'm confused where gcc optimization could break my code by cacheing pointers to the list items (next most recent item to remove, list head, or free list). I dont want

java Volatile/synchronization on arraylist

白昼怎懂夜的黑 提交于 2019-12-07 13:01:14
问题 My program looks like this: public class Main { private static ArrayList<T> list; public static void main(String[] args) { new DataListener().start(); new DataUpdater().start(); } static class DataListener extends Thread { @Override public void run() { while(true){ //Reading the ArrayList and displaying the updated data Thread.sleep(5000); } } } static class DataUpdater extends Thread{ @Override public void run() { //Continuously receive data and update ArrayList; } } } In order to use this