volatile

Example of C++ “Memory barrier” [duplicate]

旧街凉风 提交于 2019-12-04 03:32:27
This question already has answers here : Closed 6 years ago . C++ Memory Barriers for Atomics (2 answers) I was reading the answer to this question regarding the volatile keyword: https://stackoverflow.com/a/2485177/997112 The person says: The solution to preventing reordering is to use a memory barrier, which indicates both to the compiler and the CPU that no memory access may be reordered across this point. Placing such barriers around our volatile variable access ensures that even non-volatile accesses won't be reordered across the volatile one, allowing us to write thread-safe code.

Why readonly and volatile modifiers are mutually exclusive?

元气小坏坏 提交于 2019-12-04 03:01:42
I have a reference-type variable that is readonly , because the reference never change, only its properties. When I tried to add the volatile modifier to it the compiled warned me that it wouldn't let both modifiers apply to the same variable. But I think I need it to be volatile because I don't want to have caching problems when reading its properties. Am I missing anything? Or is the compiler wrong? Update As Martin stated in one of the comments below: Both readonly and volatile modifiers apply only to the reference, and not to the object's properties, in the case of reference-type objects.

2D Volatile arrays: will self-assignment help or do I need AtomicIntegerArray?

女生的网名这么多〃 提交于 2019-12-04 02:50:36
问题 I'm writing an audio DSP application and I've opted to use a producer-consumer model. I've been reading a lot about volatile and other threading issues, but I've got a couple of questions about some specifics of my case - particularly, one of the things I need to be shared between threads is an array of arrays. I have a class which represents the producer. To allow for variation in processing time the producer stores n buffers, which it will fill in rotation every time more audio data is

Does volatile influence non-volatile variables?

两盒软妹~` 提交于 2019-12-04 02:31:25
Okay, suppose I have a bunch of variables, one of them declared volatile: int a; int b; int c; volatile int v; If one thread writes to all four variables (writing to v last), and another thread reads from all four variables (reading from v first), does that second thread see the values written to a , b and c by the first thread, even though they are not themselves declared volatile? Or can it possibly see stale values? Since there seems to be some confusion: I'm not deliberately trying to do something unsafe. I just want to understand the Java memory model and the semantics of the volatile

C# bool is atomic, why is volatile valid

耗尽温柔 提交于 2019-12-04 01:55:03
In C# , we know that a bool is atomic - then why is it valid to mark it as volatile ? what is the difference and what is a good (or even practical) use-case for one versus the other? bool _isPending; Versus volatile bool _isPending; // Is this realistic, or insanity? I have done some reading here and here , and I'm trying to ensure that I fully understand the inner workings of the two. I want to understand when it is appropriate to use one vs the other, or if just bool is enough. Eric Lippert In C#, we know that a bool is atomic - then why is it valid to mark it as volatile? what is the

HashMap、ConcurrentHashMap解析

女生的网名这么多〃 提交于 2019-12-04 01:44:28
一、HashMap分析   在JDK1.8之前,hashMap由数组+链表组成,1.8之后,对hashMap进行了一些修改,最大的不同就是利用了红黑树,所以其由数组+链表+红黑树组成。查找时,根据hash值我们能够快速定位到数组的具体下标,但是之后的话,需要顺着链表一个个比较下去才能找到我们需要的,时间复杂度取决于链表的长度为O(n),为了降低这部分的开销,在Java8中,当链表中的元素达到了8个时,会将链表转换为红黑树,在这些位置进行查找时可以降低时间复杂度为O(logn)。 1.put过程:(JDK1.8)    第一次put值时,会触发resize(),类似Java7的第一次put也是要初始化数组长度的。    第一次resize和后续的扩容有些不一样,因为这次是数组从null初始化到默认的16或自定义的初始容量,找到具体的数据下标,如果此位置没有值,那么直接初始化一下Node并放置在这个位置就可以了。如果数组改为只有数据:首先,判断该位置的第一个数据和我们要插入的数据,key是不是“相等”,如果是,取出这个节点,如果该节点是代表红黑树的节点,调用红黑树的插值方法,插入到链表的最后面(Java7是插入到链表的最前面),当treeify_threshold为8时,如果新插入的值是链表中的第8个,会触发下面的treeifyBin,也就是将链表转换为红黑树;如果在该链表中找到了

多线程并发编程总结(一)

為{幸葍}努か 提交于 2019-12-04 01:39:51
多线程的优缺点 多线程的优点: 资源利用率更好, 程序响应更快。 多线程的代价: 设计复杂, 上下文切换开销大(先存储当前线程的本地的数据,程序指针等,然后载入另一个线程的本地数据,程序指针等,最后才开始执行), 增加资源消耗(每个线程需要消耗的资源)。 线程的状态 new(新建) runnnable(可运行) running(运行) blocked(阻塞) waiting(等待) time waiting (定时等待) terminated(终止) JMM(Java内存模型) JMM定义了线程和主内存之间的抽象关系。 主内存是共享内存区域,所有线程都可以访问,但线程对变量的操作(读取赋值等)必须在工作内存(栈空间)中进行。 线程间的通信(传值)必须通过主内存来完成。 对于一个实例对象中的成员方法而言: 如果方法中包含本地变量是基本数据类型,将直接存储在工作内存的帧栈结构中, 但倘若本地变量是引用类型,那么该变量的引用会存储在功能内存的帧栈中,而对象实例将存储在主内存(共享数据区域,堆)中。 volatile写-读的内存语义 当写一个volatile变量时,JMM会把该线程对应的本地内存中的共享变量刷新到主内存。 当读一个volatile变量时,JMM会把该线程对应的本地内存置为无效。线程接下来将从主内存中读取共享变量。 锁释放和获取的内存语义 当线程释放锁时

Why is “volatileQualifiedExpr + volatileQualifiedExpr” not necessarily UB in C but in C++?

对着背影说爱祢 提交于 2019-12-04 01:38:37
When I today read the C Standard, it says about side effects Accessing a volatile object, modifying an object, modifying a file, or calling a function that does any of those operations are all side effects and the C++ Standard says Accessing an object designated by a volatile glvalue (3.10), modifying an object, calling a library I/O function, or calling a function that does any of those operations are all side effects Hence because both forbid unsequenced side effects to occur on the same scalar object, C allows the following, but C++ makes it undefined behavior int a = 0; volatile int *pa =

快手面试归来篇一,希望你看后能过

僤鯓⒐⒋嵵緔 提交于 2019-12-04 00:27:25
目录 写给正在找工作的你 快手面试 算法 基础 写给正在找工作的你 都说金三银四,对于找工作的人来说,因为每年的三月或四月是不少互联网公司的年终季,不少人都是拿到年终奖后不满意,或者感觉职业发展受限,之后跑路。这样不少部门因为人员流动,就会有hc空缺出来。 ==这里要说的是每年3、4月份确实是hc最多的季节,但同时是跳槽旺季,竞争大,你要想找到好的坑位,那就需要绝对的实力才行。== 相对来说,其实年底是个好时候,俗话说,铁打的营盘流水的兵,互联网的阵地上不少岗位是常年招人,常年缺人,当然hc并不富裕,但是年底的时候,看机会的人也少。毕竟不少人还是很在意“年终奖”的嘛。所以说,年底跳槽你可能会损失一部分年终奖,但换工作的竞争性相对来说也会少很多,竞争的人少了嘛,说不定你就可以凭“运气的实力”脱颖而出呢? 快手面试 算法 面试官很亲切,说Excel表用过吧,Excel表中的编号一般是这样的,A....Z AA...AZ BA...BZ,分别对应数字0...25 26....51 52...77,类比做数字映射,给出一个字符串,求映射的结果。 分析 这个题目其实很基础,可以理解为是以26为基准的进制转换,一个for循环,除了末尾的字符直接加到结果上之外,其他的字符位-'A'+1的结果乘以26*(该字符位置与末尾的差值)。做这种题目一定要先思考,自己手动实现一下。 如果想看具体代码答案

详解C中volatile关键字

五迷三道 提交于 2019-12-03 23:43:13
在PHP官网上看到一个浮点数BUG, 测试代码 。在SVN里看了一下修复方法:在变量的声明前加了个volatile关键字。不知道这个是什么意思,特意去网上查了一下,找到了这篇文章,写得不错,转载一下。 volatile提醒编译器它后面所定义的变量随时都有可能改变,因此编译后的程序每次需要存储或读取这个变量的时候,都会直接从变量地址中读取数据。如 果没有volatile关键字,则编译器可能优化读取和存储,可能暂时使用寄存器中的值,如果这个变量由别的程序更新了的话,将出现不一致的现象。下面举 例说明。在DSP开发中,经常需要等待某个事件的触发,所以经常会写出这样的程序: short flag; void test() { do1(); while(flag==0); do2(); } 这段程序等待内存变量flag的值变为1(怀疑此处是0,有点疑问,)之后才运行do2()。变量flag的值由别的程序更改,这个程序可能是某个硬件中 断服务程序。例如:如果某个按钮按下的话,就会对DSP产生中断,在按键中断程序中修改flag为1,这样上面的程序就能够得以继续运行。但是,编译器并 不知道flag的值会被别的程序修改,因此在它进行优化的时候,可能会把flag的值先读入某个寄存器,然后等待那个寄存器变为1。如果不幸进行了这样的 优化,那么while循环就变成了死循环