最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

C++中volatile和mutable關(guān)鍵字用法詳解

 更新時(shí)間:2020年02月01日 14:03:05   作者:fengbingchun  
這篇文章主要介紹了C++中volatile和mutable關(guān)鍵字用法詳解,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧

C/C++中的volatile關(guān)鍵字和const對(duì)應(yīng),用來修飾變量,用于告訴編譯器該變量值是不穩(wěn)定的,可能被更改。使用volatile注意事項(xiàng):

(1). 編譯器會(huì)對(duì)帶有volatile關(guān)鍵字的變量禁用優(yōu)化(A volatile specifier is a hint to a compiler that an object may change its value in ways not specified by the language so that aggressive optimizations must be avoided)。

(2). 當(dāng)多個(gè)線程都要用到某一個(gè)變量且該變量的值會(huì)被改變時(shí)應(yīng)該用volatile聲明,該關(guān)鍵字的作用是防止編譯器優(yōu)化把變量從內(nèi)存裝入CPU寄存器中。如果變量被裝入寄存器,那么多個(gè)線程有可能有的使用內(nèi)存中的變量,有的使用寄存器中的變量,這會(huì)造成程序的錯(cuò)誤執(zhí)行。volatile的意思是讓編譯器每次操作該變量時(shí)一定要從內(nèi)存中取出,而不是使用已經(jīng)存在寄存器中的值(It cannot cache the variables in register)。

(3). 中斷服務(wù)程序中訪問到的變量最好帶上volatile。

(4). 并行設(shè)備的硬件寄存器的變量最好帶上volatile。

(5). 聲明的變量可以同時(shí)帶有const和volatile關(guān)鍵字。

(6). 多個(gè)volatile變量間的操作,是不會(huì)被編譯器交換順序的,能夠保證volatile變量間的順序性,編譯器不會(huì)進(jìn)行亂序優(yōu)化(The value cannot change in order of assignment)。但volatile變量和非volatile變量之間的順序,編譯器不保證順序,可能會(huì)進(jìn)行亂序優(yōu)化。

C++中的mutable關(guān)鍵字使用場(chǎng)景

(1). 允許即使包含它的對(duì)象被聲明為const時(shí)仍可修改聲明為mutable的類成員(sometimes there is requirement to modify one or more data members of class/struct through const function even though you don't want the function to update other members of class/struct. This task can be easily performed by using mutable keyword)。

(2). 應(yīng)用在C++11 lambda表達(dá)式來表示按值捕獲的值是可修改的,默認(rèn)情況下是不可修改的,但修改僅在lambda式內(nèi)有效(since c++11 mutable can be used on a lambda to denote that things captured by value are modifiable (they aren't by default))。

詳細(xì)用法見下面的測(cè)試代碼,下面是從其他文章中copy的測(cè)試代碼,詳細(xì)內(nèi)容介紹可以參考對(duì)應(yīng)的reference:

#include "volatile_mutable.hpp"
#include <iostream>
#include <stdio.h>
#include <time.h>
#include <mutex>
#include <string.h>
 
namespace volatile_mutable_ {
 
///////////////////////////////////////////////////////////
int test_volatile_1()
{
 volatile int i1 = 0; // correct
 int volatile i2 = 0; // correct
 
 return 0;
}
 
///////////////////////////////////////////////////////////
// reference: https://en.cppreference.com/w/c/language/volatile
int test_volatile_2()
{
{ // Any attempt to read or write to an object whose type is volatile-qualified through a non-volatile lvalue results in undefined behavior
 volatile int n = 1; // object of volatile-qualified type
 int* p = (int*)&n;
 int val = *p; // undefined behavior in C, Note: link does not report an error under C++
 fprintf(stdout, "val: %d\n", val);
}
 
{ // A member of a volatile-qualified structure or union type acquires the qualification of the type it belongs to
 typedef struct ss { int i; const int ci; } s;
 // the type of s.i is int, the type of s.ci is const int
 volatile s vs = { 1, 2 };
 // the types of vs.i and vs.ci are volatile int and const volatile int
}
 
{ // If an array type is declared with the volatile type qualifier (through the use of typedef), the array type is not volatile-qualified, but its element type is
 typedef int A[2][3];
 volatile A a = { {4, 5, 6}, {7, 8, 9} }; // array of array of volatile int
 //int* pi = a[0]; // Error: a[0] has type volatile int*
 volatile int* pi = a[0];
}
 
{ // A pointer to a non-volatile type can be implicitly converted to a pointer to the volatile-qualified version of the same or compatible type. The reverse conversion can be performed with a cast expression
 int* p = nullptr;
 volatile int* vp = p; // OK: adds qualifiers (int to volatile int)
 //p = vp; // Error: discards qualifiers (volatile int to int)
 p = (int*)vp; // OK: cast
}
 
{ // volatile disable optimizations
 clock_t t = clock();
 double d = 0.0;
 for (int n = 0; n < 10000; ++n)
 for (int m = 0; m < 10000; ++m)
  d += d * n*m; // reads and writes to a non-volatile 
 fprintf(stdout, "Modified a non-volatile variable 100m times. Time used: %.2f seconds\n", (double)(clock() - t) / CLOCKS_PER_SEC);
 
 t = clock();
 volatile double vd = 0.0;
 for (int n = 0; n < 10000; ++n)
 for (int m = 0; m < 10000; ++m)
  vd += vd * n*m; // reads and writes to a volatile 
 fprintf(stdout, "Modified a volatile variable 100m times. Time used: %.2f seconds\n", (double)(clock() - t) / CLOCKS_PER_SEC);
}
 
 return 0;
}
 
///////////////////////////////////////////////////////////
// reference: https://en.cppreference.com/w/cpp/language/cv
int test_volatile_3()
{
 int n1 = 0;      // non-const object
 const int n2 = 0;   // const object
 int const n3 = 0;   // const object (same as n2)
 volatile int n4 = 0; // volatile object
 const struct {
 int n1;
 mutable int n2;
 } x = { 0, 0 };   // const object with mutable member
 
 n1 = 1; // ok, modifiable object
 //n2 = 2; // error: non-modifiable object
 n4 = 3; // ok, treated as a side-effect
 //x.n1 = 4; // error: member of a const object is const
 x.n2 = 4; // ok, mutable member of a const object isn't const
 
 const int& r1 = n1; // reference to const bound to non-const object
 //r1 = 2; // error: attempt to modify through reference to const
 const_cast<int&>(r1) = 2; // ok, modifies non-const object n1
 fprintf(stdout, "n1: %d\n", n1); // 2
 
 const int& r2 = n2; // reference to const bound to const object
 //r2 = 2; // error: attempt to modify through reference to const
 const_cast<int&>(r2) = 2; // undefined behavior: attempt to modify const object n2, Note: link does not report an error under C++
 fprintf(stdout, "n2: %d\n", n2); // 0
 
 return 0;
}
 
///////////////////////////////////////////////////////////
// reference: https://www.geeksforgeeks.org/understanding-volatile-qualifier-in-c/
int test_volatile_4()
{
{
 const int local = 10;
 int *ptr = (int*)&local;
 fprintf(stdout, "Initial value of local : %d \n", local); // 10
 
 *ptr = 100;
 fprintf(stdout, "Modified value of local: %d \n", local); // 10
}
 
{
 const volatile int local = 10;
 int *ptr = (int*)&local;
 fprintf(stdout, "Initial value of local : %d \n", local); // 10
 
 *ptr = 100;
 fprintf(stdout, "Modified value of local: %d \n", local); // 100
}
 
 return 0;
}
 
///////////////////////////////////////////////////////////
// reference: https://en.cppreference.com/w/cpp/language/cv
int test_mutable_1()
{
 // Mutable is used to specify that the member does not affect the externally visible state of the class (as often used for mutexes,
 // memo caches, lazy evaluation, and access instrumentation)
 class ThreadsafeCounter {
 public:
 int get() const {
  std::lock_guard<std::mutex> lk(m);
  return data;
 }
 void inc() {
  std::lock_guard<std::mutex> lk(m);
  ++data;
 }
 
 private:
 mutable std::mutex m; // The "M&M rule": mutable and mutex go together
 int data = 0;
 };
 
 return 0;
}
 
///////////////////////////////////////////////////////////
// reference: https://www.tutorialspoint.com/cplusplus-mutable-keyword
int test_mutable_2()
{
 class Test {
 public:
 Test(int x = 0, int y = 0) : a(x), b(y) {}
 
 void seta(int x = 0) { a = x; }
 void setb(int y = 0) { b = y; }
 void disp() { fprintf(stdout, "a: %d, b: %d\n", a, b); }
 
 public:
 int a;
 mutable int b;
 };
 
 const Test t(10, 20);
 fprintf(stdout, "t.a: %d, t.b: %d \n", t.a, t.b); // 10, 20
 
 //t.a=30; // Error occurs because a can not be changed, because object is constant.
 t.b = 100; // b still can be changed, because b is mutable.
 fprintf(stdout, "t.a: %d, t.b: %d \n", t.a, t.b); // 10, 100
 
 return 0;
}
 
///////////////////////////////////////////////////////////
// reference: https://www.geeksforgeeks.org/c-mutable-keyword/
int test_mutable_3()
{
 using std::cout;
 using std::endl;
 
 class Customer {
 public:
 Customer(char* s, char* m, int a, int p)
 {
  strcpy(name, s);
  strcpy(placedorder, m);
  tableno = a;
  bill = p;
 }
 
 void changePlacedOrder(char* p) const { strcpy(placedorder, p); }
 void changeBill(int s) const { bill = s; }
 
 void display() const
 {
  cout << "Customer name is: " << name << endl;
  cout << "Food ordered by customer is: " << placedorder << endl;
  cout << "table no is: " << tableno << endl;
  cout << "Total payable amount: " << bill << endl;
 }
 
 private:
 char name[25];
 mutable char placedorder[50];
 int tableno;
 mutable int bill;
 };
 
 const Customer c1("Pravasi Meet", "Ice Cream", 3, 100);
 c1.display();
 c1.changePlacedOrder("GulabJammuns");
 c1.changeBill(150);
 c1.display();
 
 return 0;
}
 
///////////////////////////////////////////////////////////
// reference: https://stackoverflow.com/questions/105014/does-the-mutable-keyword-have-any-purpose-other-than-allowing-the-variable-to
int test_mutable_4()
{
 int x = 0;
 auto f1 = [=]() mutable { x = 42; }; // OK
 //auto f2 = [=]() { x = 42; }; // Error: a by-value capture cannot be modified in a non-mutable lambda
 fprintf(stdout, "x: %d\n", x); // 0
 
 return 0;
}
 
} // namespace volatile_mutable_

GitHub:https://github.com/fengbingchun/Messy_Test

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • C++ 流插入和流提取運(yùn)算符的重載的實(shí)現(xiàn)

    C++ 流插入和流提取運(yùn)算符的重載的實(shí)現(xiàn)

    這篇文章主要介紹了C++ 流插入和流提取運(yùn)算符的重載的實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2019-12-12
  • FFmpeg實(shí)戰(zhàn)之利用ffplay實(shí)現(xiàn)自定義輸入流播放

    FFmpeg實(shí)戰(zhàn)之利用ffplay實(shí)現(xiàn)自定義輸入流播放

    ffplay是FFmpeg提供的一個(gè)極為簡(jiǎn)單的音視頻媒體播放器,可以用于音視頻播放、可視化分析。本文將利用ffplay實(shí)現(xiàn)自定義輸入流播放,需要的可以參考一下
    2022-12-12
  • 淺析C++中類模板的用法

    淺析C++中類模板的用法

    C++類模板是一種用于創(chuàng)建通用類的工具,它允許我們定義一個(gè)通用類,支持多種類型。本文就來簡(jiǎn)單講講它的具體使用吧,感興趣的可以了解一下
    2023-04-04
  • C++?通過pqxxlib庫(kù)鏈接?PostgreSql數(shù)據(jù)庫(kù)的詳細(xì)過程

    C++?通過pqxxlib庫(kù)鏈接?PostgreSql數(shù)據(jù)庫(kù)的詳細(xì)過程

    這篇文章主要介紹了C++?通過pqxxlib庫(kù)鏈接?PostgreSql數(shù)據(jù)庫(kù),本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2023-04-04
  • C/C++ assert()函數(shù)用法案例總結(jié)

    C/C++ assert()函數(shù)用法案例總結(jié)

    這篇文章主要介紹了C/C++ assert()函數(shù)用法案例總結(jié),本篇文章通過簡(jiǎn)要的案例,講解了該項(xiàng)技術(shù)的了解與使用,以下就是詳細(xì)內(nèi)容,需要的朋友可以參考下
    2021-09-09
  • C++中圖片重命名實(shí)現(xiàn)代碼

    C++中圖片重命名實(shí)現(xiàn)代碼

    這篇文章主要介紹了C++中圖片重命名實(shí)現(xiàn)代碼,需要的朋友可以參考下
    2017-05-05
  • Qt編寫地圖之實(shí)現(xiàn)經(jīng)緯度坐標(biāo)糾偏

    Qt編寫地圖之實(shí)現(xiàn)經(jīng)緯度坐標(biāo)糾偏

    地圖應(yīng)用中都涉及到一個(gè)問題就是坐標(biāo)糾偏的問題,這個(gè)問題的是因?yàn)楦鶕?jù)地方規(guī)則保密性要求不允許地圖廠商使用標(biāo)準(zhǔn)的GPS坐標(biāo),而是要用國(guó)家定義的偏移標(biāo)準(zhǔn)。本文將詳細(xì)講解如何在Qt中實(shí)現(xiàn)經(jīng)緯度坐標(biāo)糾偏,需要的可以參考一下
    2022-03-03
  • 如何求連續(xù)幾個(gè)數(shù)之和的最大值

    如何求連續(xù)幾個(gè)數(shù)之和的最大值

    本篇文章是對(duì)如何求連續(xù)幾個(gè)數(shù)之和的最大值進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下
    2013-05-05
  • 詳解如何在C/C++中測(cè)量一個(gè)函數(shù)或功能的運(yùn)行時(shí)間

    詳解如何在C/C++中測(cè)量一個(gè)函數(shù)或功能的運(yùn)行時(shí)間

    本文算是一個(gè)比較完整的關(guān)于在 C/C++ 中測(cè)量一個(gè)函數(shù)或者功能的總結(jié),最后會(huì)演示三種方法的對(duì)比,文章通過代碼示例給大家介紹的非常詳細(xì),需要的朋友可以參考下
    2023-12-12
  • VScode配置cuda開發(fā)環(huán)境的實(shí)現(xiàn)步驟

    VScode配置cuda開發(fā)環(huán)境的實(shí)現(xiàn)步驟

    本文主要介紹了VScode配置cuda開發(fā)環(huán)境的實(shí)現(xiàn)步驟,文中通過圖文介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2024-07-07

最新評(píng)論

沙田区| 宁乡县| 定西市| 虎林市| 井研县| 门源| 巴南区| 襄垣县| 扎赉特旗| 沅江市| 赫章县| 壤塘县| 文昌市| 桐梓县| 衡阳县| 长汀县| 东宁县| 甘德县| 长寿区| 泉州市| 成安县| 额敏县| 会宁县| 延安市| 龙山县| 德保县| 四子王旗| 卓尼县| 百色市| 灵丘县| 平山县| 田阳县| 额尔古纳市| 呼伦贝尔市| 淄博市| 漠河县| 溆浦县| 安溪县| 白银市| 五台县| 绵竹市|