volatile

Is the volatile mark necessary for a queue with one thread adding and another reading?

放肆的年华 提交于 2019-12-24 19:48:49
问题 in response to some comments in this thread, I am wondering if making a queue that will have one thread adding to it and another thread reading from it requires the 'volatile' mark? I suppose most cases would be alright, but in the case of reading and writing in a queue of size 1 could cause issues without it? Goal: a thread safe queue where I can post messages to be sent over a TCP socket connection. The out stream will be managed by another class who will check if the queue is empty at the

Volatile specifier ignored in C++

﹥>﹥吖頭↗ 提交于 2019-12-24 16:26:11
问题 I'm pretty new to C++ and recently I ran across some info on what it means for a variable to be volatile . As far as I understood, it means a read or write to the variable can never be optimized out of existence. However a weird situation arises when I declare a volatile variable that isn't 1, 2, 4, 8 bytes large: the compiler(gnu with C++11 enabled) seemingly ignores the volatile specifier #define expand1 a, a, a, a, a, a, a, a, a, a #define expand2 // ten expand1 here, expand3 to expand5

Assigning a volatile rvalue

若如初见. 提交于 2019-12-24 14:18:27
问题 I don't understand why the following code doesn't compile: #include <iostream> class Test { public: Test() { std::cout << "Constructor" << std::endl; } Test(const Test&) { std::cout << "Copy Constructor" << std::endl; } Test& operator=(const Test&) { std::cout << "Assign Op" << std::endl; return *this; } Test& operator=(const volatile Test&) { std::cout << "Volatile Assign Op" << std::endl; return *this; } }; volatile Test func() { Test a; return a; } int main() { Test b; volatile Test c; b =

Casting the results of a volatile expression to void

梦想的初衷 提交于 2019-12-24 11:08:24
问题 Note: This is Not the same thing that has been asked many times. Yes I have read the many many posts about casting to void. None of those questions resulted in the answer I suspect is true here. Background info: Embedded C. This is specifically related to memory mapped volatile pointers. In other words, peripheral registers. I came across the following line in a routine that involves writing to an I2C peripheral: (void) I2C1->SR2; I2C1 is #defined as a struct * to volatile memory. So the

java中volatile关键字的含义

拟墨画扇 提交于 2019-12-24 11:04:39
在java线程并发处理中,有一个关键字volatile的使用目前存在很大的混淆,以为使用这个关键字,在进行多线程并发处理的时候就可以万事大吉。 Java语言是支持多线程的,为了解决线程并发的问题,在语言内部引入了 同步块 和 volatile 关键字机制。 synchronized 同步块大家都比较熟悉,通过 synchronized 关键字来实现,所有加上synchronized 和 块语句,在多线程访问的时候,同一时刻只能有一个线程能够用 synchronized 修饰的方法 或者 代码块。 volatile 用volatile修饰的变量,线程在每次使用变量的时候,都会读取变量修改后的最的值。volatile很容易被误用,用来进行原子性操作。 下面看一个例子,我们实现一个计数器,每次线程启动的时候,会调用计数器inc方法,对计数器进行加一 public class Counter { public static int count = 0; public static void inc() { //这里延迟1毫秒,使得结果明显 try { Thread.sleep(1); } catch (InterruptedException e) { } count++; } public static void main(String[] args) { //同时启动1000个线程

Use of volatile for registers

寵の児 提交于 2019-12-24 09:34:10
问题 I am wondering how efficient / is working the volatile declaration. In the following code: volatile char var1 = * (volatile char *) 0x2000000; printf("%d \n", var1 + 1); It means that every time I am using the variable "var", it will be loaded from the address memory 0x2000000, but is the volatile cast of a fixed address necessary, or is it only to fit the type of var1? Does this means that every time "var1" is read, it is read from memory, and not from a potential cached value? So in this

synchronize on different object visibility

主宰稳场 提交于 2019-12-24 09:24:45
问题 the following code shows synchronization on different object than this: public class A { int a,b,c,d; public void method1(Object x){ synchronized(x){ // is a ,b ,c ,d guarantee visibility ? } } public synchronized void method2() { a++; } } I know there will be a problem to edit a , b ,c ,d with having different lock in method1 and method2 , but the question is the changes flushed by method2 be visible to method1? because they don't use the same lock. 回答1: If you only read a , on x64 this will

java高级面试题总结

青春壹個敷衍的年華 提交于 2019-12-24 05:10:44
目录 1、java基础 1.1、hashmap原理?扩容 1.2、arraylist原理?扩容 1.3、jdk1.8新特性? 1.4、completablefuture 2、数据库 2.1、mysql索引优化 2.2、创建索引的依据? 2.3、mysql执行计划,explain各项参数代表什么意思? 2.4、什么时候不会用到索引? 2.5、mysql存储引擎 2.6、数据库隔离级别 2.7、为什么要使用索引 3、java多线程 3.1、java并发包java.util.concurrent及其子包都包括什么? 3.2、synconsized和volatile关键字区别? 3.3、实现线程池的方式? 3.4、公平锁与非公平锁 3.5、为什么不适用Excutors来创建线程池 3.6、ReentraneLock & AQS 4、jvm、java内存模型 4.1、jvm内存模型? 4.2、jvm调优具体调的那些参数? 5、java框架(spring boot,sprint cloud) 5.1、mybatis一级缓存,二级缓存 5.2、mybatis $ # 区别? 5.3、sprint cloud 的常用组件? 6、中间件 6.1、redis存储的数据类型 6.2、redis实现分布式锁原理,使用redis实现分布式锁有什么问题? 6.3、zookeeper实现分布式锁原理 6.4

Java,该学什么?

被刻印的时光 ゝ 提交于 2019-12-24 04:29:44
本人大学学的是生物技术专业,毕业后入坑Java。 最近有人问我是如何转行的,需要学一些什么。我在网上看到一篇帖子,觉得写得很全。如果是我来写,可能还写不了这么全的。在此分享给网友。 2019秋招几个月累积的知识点,东西太多,懒得重整理做索引,尽量用(*)和 加粗 标注出高频知识点, 都是面试问过的或笔试考过的 Java基础知识(*) https://blog.csdn.net/qq_16633405/article/details/79211002 Spring Boot 启动 流程(*) https://juejin.im/post/5b679fbc5188251aad213110#heading-0 Spring 一些面试题(*) https://www.ctolib.com/topics-35589.html 匿名内部类编译class(*) https://blog.csdn.net/lazyer_dog/article/details/50669473 为什么集合类没有实现Cloneable和Serializable接口? https://www.nowcoder.com/questionTerminal/2a4902f67d5b49b6b4c05f9d7e422caf 自动装箱原理 https://www.jianshu.com/p/0ce2279c5691

Can I use mutex or key word(static) instead of volatile in C++?

一个人想着一个人 提交于 2019-12-24 01:33:14
问题 I know how volatile works in C++. However, I'm still feeling confused about why we need volatile. Only reason I think is we need listen whether the variable changed, as the code shows below: volatile int some_int = 100; while(some_int==100) { doSomething() } The programmer needs know if the word some_int changed, so he uses "volatile". Could I use the key word "static" instead? Mostly, I think we should we mutex(lock) to synchronize if synchronization is necessary in multi-thread. Excuse my