zoukankan      html  css  js  c++  java
  • volatile

    In computer programming, particularly in the CC++C#, and Java programming languages, a variable or object declared with thevolatile keyword usually has special properties related to optimization and/or threading. Generally speaking, the volatile keyword is intended to prevent the compiler from applying certain optimizations which it might have otherwise applied because ordinarily it is assumed variables cannot change value "on their own."

    The actual definition and applicability of the volatile keyword is often misconstrued in the context of the C language. Although C++, C#, and Java share the same keyword volatile from C, there is a great deal of difference between the semantics and usefulness ofvolatile in each of these programming languages.

    In C, and consequently C++, the volatile keyword was intended to[1]

    • allow access to memory mapped devices
    • allow uses of variables between setjmp and longjmp
    • allow uses of sig_atomic_t variables in signal handlers.

    Operations on volatile variables are not atomic, nor do they establish a proper happens-before relationship for threading. This is according to the relevant standards (C, C++, POSIX, WIN32),[2] and this is the matter of fact for the vast majority of current implementations. Thus, the usage of volatile keyword as a portable synchronization mechanism is discouraged by many C/C++ groups.

    The Java programming language also has the volatile keyword, but it is used for a somewhat different purpose. When applied to a field, the Java volatile guarantees that:

    1. (In all versions of Java) There is a global ordering on the reads and writes to a volatile variable. This implies that everythread accessing a volatile field will read its current value before continuing, instead of (potentially) using a cached value. (However, there is no guarantee about the relative ordering of volatile reads and writes with regular reads and writes, meaning that it's generally not a useful threading construct.)
    2. (In Java 5 or later) Volatile reads and writes establish a happens-before relationship, much like acquiring and releasing a mutex.[9]

    Using volatile may be faster than a lock, but it will not work in some situations.[citation needed] The range of situations in which volatile is effective was expanded in Java 5; in particular, double-checked locking now works correctly.

  • 相关阅读:
    Oracle DB管理内存
    DISPLAY变量和xhost(原创)
    CentOS7下swap分区创建(添加),删除以及相关配置
    如何在linux下开启FTP服务
    linux系统下如何挂载NTFS移动硬盘
    Oracle DB 使用RMAN恢复目录
    Oracle数据库联机重定义讲解及错误处理
    linux常用命令
    iptables常用命令
    python打印详细的异常信息
  • 原文地址:https://www.cnblogs.com/reynold-lei/p/3415196.html
Copyright © 2011-2022 走看看