volatile

Atomic swap in GNU C++

匿名 (未验证) 提交于 2019-12-03 01:17:01
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 问题: I want to verify that my understanding is correct. This kind of thing is tricky so I'm almost sure I am missing something. I have a program consisting of a real-time thread and a non-real-time thread. I want the non-RT thread to be able to swap a pointer to memory that is used by the RT thread. From the docs, my understanding is that this can be accomplished in g++ with: // global Data *rt_data; Data *swap_data(Data *new_data) { #ifdef __GNUC__ // Atomic pointer swap. Data *old_d = __sync_lock_test_and_set(&rt_data, new_data); #else // Non

Does Delphi have any equivalent to C's volatile variable?

匿名 (未验证) 提交于 2019-12-03 01:14:02
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 问题: In C and C++ a variable can be marked as volatile , which means the compiler will not optimize it because it may be modified external to the declaring object. Is there an equivalent in Delphi programming? If not a keyword, maybe a work around? My thought was to use Absolute , but I wasn't sure, and that may introduce other side effects. 回答1: Short answer: no. However, I am not aware of any situation in which the conservative approach of the compiler will change the number of reads or writes if you follow this approach: When reading a cross

What are the differences between const and volatile pointer in C?

匿名 (未验证) 提交于 2019-12-03 01:12:01
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 问题: What are the differences between const and volatile pointer in C? 回答1: The difference really comes down to the difference between const and volatile . The only things these two concepts have in common is syntax. const is compiler-enforced and says "the programmer can not change this." volatile says "this data might be changed by someone else" and so the compiler will not make any assumptions about that data. Without volatile the compiler might say "I put this data from memory into a register, and since I haven't done anything to that data, I

Is the volatile keyword required for fields accessed via a ReentrantLock?

烂漫一生 提交于 2019-12-03 01:11:12
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; public synchronized void method() { double temp = sharedData; temp *= 2.5; sharedData = temp + 1; } } For next example using a ReentrantLock however, is the volatile keyword on the field necessary? class B { private final ReentrantLock lock = new ReentrantLock();

What does “cv-unqualified” mean in C++?

匿名 (未验证) 提交于 2019-12-03 01:06:02
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 问题: As from subject. I saw this terminology in a question I recently asked, and apparently it's a well established term, but I am not able to find anything on stackoverflow. 回答1: There are fundamental types and compound types. Fundamental types are the arithmetic types, void , and std::nullptr_t . Compound types are arrays, functions, pointers, references, classes, unions, enumerations, and pointers to non-static members. A cv-unqualified type is any of those types. For any cv-unqualified type, there are three corresponding cv-qualified types:

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

匿名 (未验证) 提交于 2019-12-03 00:56:02
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 问题: 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 my global variable is volatile, it could

volatile关键字解析

匿名 (未验证) 提交于 2019-12-03 00:40:02
这个可见性是指,当一个线程读取volatile修饰的变量时,永远读取的都是最后一个线程写回主内存的最新值,某个线程在读取数据之后,其他线程对变量值做了修改,这个线程是不知道的,这就导致当前线程读取的值是过期的,当前线程将过期的数据经过计算写会主内存时,就会出现问题。看下面代码: public class VolatileTest { public static volatile int race = 0; /** * 每次都对race累加 */ public static void increase() { race++; } private static int THREAD_COUNT = 20; public static void main(String[] args) { // start 20 thread, every thread invoke increase function 20 times for (int tCount = 0; tCount < THREAD_COUNT; tCount++) { new Thread(() -> { for (int i = 0; i < 10000; i++) { increase(); } System.out.println(Thread.currentThread().getName() + "is

深入分析volatile的实现原理

匿名 (未验证) 提交于 2019-12-03 00:33:02
Java编程语言允许线程访问共享变量,为了确保共享变量能被准确和一致地更新,线程应该确保通过排他锁单独获得这个变量。 内存模型相关概念 操作系统语义 1 i++i++ Java内存模型 原子性 原子性:即一个操作或者多个操作 要么全部执行并且执行的过程不会被任何因素打断,要么就都不执行。 1 2 3 4 i = 0 ; --- 1 j = i ; --- 2 i++; --- 3 i = j + 1 ; --- 4 volatile是无法保证复合操作的原子性 可见性 可见性是指当多个线程访问同一个变量时,一个线程修改了这个变量的值,其他线程能够立即看得到修改的值。 有序性 有序性:即程序执行的顺序按照代码的先后顺序执行。 剖析volatile原理 volatile可以保证线程可见性且提供了一定的有序性,但是无法保证原子性。在JVM底层volatile是采用“内存屏障”来实现的。 总结 volatile经常用于两个两个场景:状态标记两、double check 参考资料 转载请标明出处: 深入分析volatile的实现原理 文章来源: 深入分析volatile的实现原理

Volatile详解

匿名 (未验证) 提交于 2019-12-03 00:21:02
之前研究了一下内存模型,java内存模型中很关键的一点就是Volatile,现在跟大家探讨一下Volatile的知识吧 1.volatile关键字的两层语义   一旦一个共享变量(类的成员变量、类的静态成员变量)被volatile修饰之后,那么就具备了两层语义:   1)保证了不同线程对这个变量进行操作时的可见性,即一个线程修改了某个变量的值,这新值对其他线程来说是立即可见的。   2)禁止进行指令重排序。   先看一段代码,假如线程1先执行,线程2后执行: 这段代码是很典型的一段代码,很多人在中断线程时可能都会采用这种标记办法。但是事实上,这段代码会完全运行正确么?即一定会将线程中断么?不一定,也许在大多数时候,这个代码能够把线程中断,但是也有可能会导致无法中断线程(虽然这个可能性很小,但是只要一旦发生这种情况就会造成死循环了)。   下面解释一下这段代码为何有可能导致无法中断线程。在前面已经解释过,每个线程在运行过程中都有自己的工作内存,那么线程1在运行的时候,会将stop变量的值拷贝一份放在自己的工作内存当中。   那么当线程2更改了stop变量的值之后,但是还没来得及写入主存当中,线程2转去做其他事情了,那么线程1由于不知道线程2对stop变量的更改,因此还会一直循环下去。   但是用volatile修饰之后就变得不一样了:   第一

C语言 关键字说明

匿名 (未验证) 提交于 2019-12-03 00:19:01
1数据类型关键字(12个): (1)char:声明字符型变量或函数 (2)double:声明双精度变量或函数 (3)enum:声明枚举类型 (4)float:声明浮点型变量或函数 (5)int:声明整型变量或函数 (6)long:声明长整型变量或函数 (7)short:声明短整型变量或函数 (8)signed:声明有符号类型变量或函数 (9)struct:声明结构体变量或函数 (10)union:声明共用体(联合)数据类型 (11)unsigned:声明无符号类型变量或函数 (12)void:声明函数无返回值或无参数,声明无类型指针(基本上就这三个作用) 2控制语句关键字(12个): A循环语句 (1)for:一种循环语句(可意会不可言传) (2)do:循环语句的循环体 (3)while:循环语句的循环条件 (4)break:跳出当前循环 (5)continue:结束当前循环,开始下一轮循环 B条件语句 (1)if:条件语句 (2)else:条件语句否定分支(与if连用) (3)goto:无条件跳转语句 C开关语句 (1)switch:用于开关语句 (2)case:开关语句分支 (3)default:开关语句中的“其他”分支 D返回语句 return:子程序返回语句(可以带参数,也看不带参数) 3存储类型关键字(4个) (1)auto:声明自动变量一般不使用 (2)extern