volatile

How to copy/set a volatile std::string?

◇◆丶佛笑我妖孽 提交于 2019-12-04 14:13:30
How can I copy a volatile std::string ? There is no copy constructor for volatile, nor does something like c_str allow volatile access. operator= also doesn't seem to allow setting a volatile. It seems like std::string is simply unusable as a volatile object. Is this intended, or am I missing some way to use it? NOTE: I have easy workarounds, I just came upon the issue while trying to use string in some low-level code. As you noted, none of the member functions on std::string are marked volatile , so you can't perform any operations on a volatile std::string . I think the only option would be

What is the effect of InterlockedIncrement argument declared as volatile

最后都变了- 提交于 2019-12-04 13:51:07
InterlockedIncrement and other Interlocked operations declare their arguments as volatile. Why? What is the intention and effect of this? The probable effect is very minimal. The most likely intent is to allow users to pass volatile -qualified variables to these functions without the need for a typecast. This is done so that the function can be called both on normal variables and on volatile variables. You cannot pass a volatile variable into a function which is not expecting a volatile argument. The following code does not compile (tested with Visual Studio 2005 C++ compiler): void TestV(int

The volatile key word and memory consistency errors

我们两清 提交于 2019-12-04 13:10:54
问题 In the oracle Java documentation located here, the following is said: Atomic actions cannot be interleaved, so they can be used without fear of thread interference. However, this does not eliminate all need to synchronize atomic actions, because memory consistency errors are still possible. Using volatile variables reduces the risk of memory consistency errors, because any write to a volatile variable establishes a happens-before relationship with subsequent reads of that same variable. This

How does volatile keyword ensure an object`s fields are visible to other threads?

不想你离开。 提交于 2019-12-04 12:18:19
public class ObjectPropertiesVolatileTest { static Cup cup = new Cup(); public static void changeColor() { cup.setColor("black"); // change color of cup to black } public static void main(String[] args) { cup.setColor("red"); // initialize color of cup as red for(int i = 0; i < 3; i++) { new Thread(){ public void run() { for(int i = 0; i < 10; i++) System.out.println(Thread.currentThread().getName() + " " + i + " is " + cup.getColor()); if(Thread.currentThread().getName().equals("Thread-1")) changeColor(); // Thread-1 changes the color of cup for(int i = 0; i < 10; i++) System.out.println

Java多线程

坚强是说给别人听的谎言 提交于 2019-12-04 11:53:55
1. 多线程 image.png 新建状态: 一个新产生的线程从新状态开始了它的生命周期。它保持这个状态直到程序 start 这个线程。 运行状态:当一个新状态的线程被 start 以后,线程就变成可运行状态,一个线程在此状态下被认为是开始执行其任务 就绪状态:当一个线程等待另外一个线程执行一个任务的时候,该线程就进入就绪状态。当另一个线程给就绪状态的线程发送信号时,该线程才重新切换到运行状态。 休眠状态: 由于一个线程的时间片用完了,该线程从运行状态进入休眠状态。当时间间隔到期或者等待的时间发生了,该状态的线程切换到运行状态。 终止状态: 一个运行状态的线程完成任务或者其他终止条件发生,该线程就切换到终止状态。 2. 僵死进程 计算机的计算模型大部分是基于空间和时间来考虑的。僵死进程唯一占用的空间是pid空间,这个空间如果不能合理的应用就会造成浪费,之所以保留这个空间,是为了让父进程感知子进程已经终止这个行为。时间方面,这个感知过程是一个异步的过程。 3. 创建线程的方式 继承 Thread 类 实现 Runnable 接口 使用 Executor 框架 法一:继承Thread类 1.1定义一个类继承Thread 1.2重写run方法 1.3创建对象 1.4调用start方法开启线程 线程对象调用run()方法和start()方法区别? 调用run方法不开启线程,仅是对象调用方法

第9课 - const 和 volatile 分析

柔情痞子 提交于 2019-12-04 09:41:13
const只读变量   ·const修饰的变量是只读的,本质海是变量;   ·const修饰的局部变量在栈上分配空间;   ·const修饰的全局变量在全局数据区分配空间;   ·const只在编译期有用,在运行期无用。 const修饰的变量不是真的常量,它只是告诉编译器该变量不能出现在赋值符号的左边。 const全局变量的分歧   在现代的C语言编译器中,修改const全局变量将导致程序崩溃;   标准C语言编译器不会将const修饰的全局变量存储于只读存储器中,而是存储于可修改的全局数据区,其值依然可以改变。 #include<stdio.h> const int g_cc = 2; int main() { const int cc = 1; int* p = (int*)&cc; printf("cc = %d\n",cc); *p = 3; printf("cc = %d\n",cc); p = (int*)&g_cc; printf("g_cc = %d\n",g_cc); *p = 4; printf("g_cc = %d\n",g_cc); return 0; } const的本质   ·C语言中的const使得变量具有只读属性;   ·现代C编译器中的const将具有全局生命周期的变量存储于只读存储区;   ·const不能定义真正意义上的常量。 #include

thread safe without volatile

本小妞迷上赌 提交于 2019-12-04 09:09:30
Can anyone explain why this example is thread safe without volatile? http://www.cs.umd.edu/~pugh/java/memoryModel/DoubleCheckedLocking.html In fact, assuming that the computeHashCode function always returned the same result and had no side effects (i.e., idempotent), you could even get rid of all of the synchronization. // Lazy initialization 32-bit primitives // Thread-safe if computeHashCode is idempotent class Foo { private int cachedHashCode = 0; public int hashCode() { int h = cachedHashCode; if (h == 0) { h = computeHashCode(); cachedHashCode = h; } return h; } // other functions and

How to properly access mapped memory without undefined behavior in C++

牧云@^-^@ 提交于 2019-12-04 08:30:05
问题 I've been trying to figure out how to access a mapped buffer from C++17 without invoking undefined behavior. For this example, I'll use a buffer returned by Vulkan's vkMapMemory. So, according to N4659 (the final C++17 working draft), section [intro.object] (emphasis added): The constructs in a C++ program create, destroy, refer to, access, and manipulate objects. An object is created by a definition (6.1), by a new-expression (8.3.4), when implicitly changing the active member of a union (12

Java中的transient,volatile和strictfp关键字

℡╲_俬逩灬. 提交于 2019-12-04 08:05:06
Java中的transient,volatile和strictfp关键字 如果用transient声明一个实例变量,当对象存储时,它的值不需要维持。例如: Java代码 class T { transient int a; //不需要维持 int b; //需要维持 } 这里,如果T类的一个对象写入一个持久的存储区域,a的内容不被保存,但b的将被保存。 volatile修饰符告诉编译器被volatile修饰的变量可以被程序的其他部分改变。在多线程程序中,有时两个或更多的线程共享一个相同的实例变量。考虑效率问题,每个线程可以自己保存该共享变量的私有拷贝。实际的变量副本在不同的时候更新,如当进入synchronized方法时。 用strictfp修饰类或方法,可以确保浮点运算(以及所有切断)正如早期的Java版本那样准确。切断只影响某些操作的指数。当一个类被strictfp修饰,所有的方法自动被strictfp修饰。 strictfp的意思是FP-strict,也就是说精确浮点的意思。在Java虚拟机进行浮点运算时,如果没有指定strictfp关键字时,Java的编译器以及运行环境在对浮点运算的表达式是采取一种近似于我行我素的行为来完成这些操作,以致于得到的结果往往无法令你满意。而一旦使用了strictfp来声明一个类、接口或者方法时

一起来学习Java的Volatile关键字

我是研究僧i 提交于 2019-12-04 07:52:22
前言 在Java并发编程中,volatile关键字有着至关重要的作用,在面试中也常常会是必备的一个问题。本文将会介绍volatile关键字的作用以及其实现原理。 volatile作用 volatile在并发编程中扮演着重要的角色,volatile是轻量级的synchronized,volatile关键字有两个作用: 1)保证共享变量的可见性 可见性的意思是当一个线程修改一个共享变量时,另外一个线程能读到这个修改的值。笔者此前一篇文章Java并发编程:Java内存模型JMM中有说到,Java内存模型中有主内存和本地内存之分,本地内存持有共享变量的一份副本,线程对共享变量的修改是先修改本地内存的副本,然后再回写到主内存中去。 可能存在这样的情况,线程A和线程B同时去修改一个共享变量C,假设线程A先对共享变量C做了修改,而此时线程B却没能及时感知到共享变量C已经发生了改变,紧接着B对本地过期的副本数据进行了修改,这造成了共享变量的不可见问题。 而使用了volatile关键字修改的共享变量,当线程修改了共享变量之后,会立马刷新到主内存中,并且会使其他线程缓存了该地址的数据失效,这就保证了线程之间共享变量的可见性。 2)防止指令重排序 volatile关键字的另外一个作用就是防止指令重排序。代码在实际执行过程中,并不全是按照编写的顺序进行执行的,在保证单线程执行结果不变的情况下