日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 人文社科 > 生活经验 >内容正文

生活经验

C++中关键字volatile和mutable用法

發布時間:2023/11/27 生活经验 36 豆豆
生活随笔 收集整理的這篇文章主要介紹了 C++中关键字volatile和mutable用法 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

C/C++中的volatile關鍵字和const對應,用來修飾變量,用于告訴編譯器該變量值是不穩定的,可能被更改。使用volatile注意事項:

(1). 編譯器會對帶有volatile關鍵字的變量禁用優化(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). 當多個線程都要用到某一個變量且該變量的值會被改變時應該用volatile聲明,該關鍵字的作用是防止編譯器優化把變量從內存裝入CPU寄存器中。如果變量被裝入寄存器,那么多個線程有可能有的使用內存中的變量,有的使用寄存器中的變量,這會造成程序的錯誤執行。volatile的意思是讓編譯器每次操作該變量時一定要從內存中取出,而不是使用已經存在寄存器中的值(It cannot cache the variables in register)。

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

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

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

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

C++中的mutable關鍵字使用場景:

(1). 允許即使包含它的對象被聲明為const時仍可修改聲明為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). 應用在C++11 lambda表達式來表示按值捕獲的值是可修改的,默認情況下是不可修改的,但修改僅在lambda式內有效(since c++11 mutable can be used on a lambda to denote that things captured by value are modifiable (they aren't by default))。

詳細用法見下面的測試代碼,下面是從其他文章中copy的測試代碼,詳細內容介紹可以參考對應的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; // correctint volatile i2 = 0; // correctreturn 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 behaviorvolatile int n = 1; // object of volatile-qualified typeint* 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 totypedef struct ss { int i; const int ci; } s;// the type of s.i is int, the type of s.ci is const intvolatile 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 istypedef 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 expressionint* 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 optimizationsclock_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 objectconst int n2 = 0;     // const objectint const n3 = 0;     // const object (same as n2)volatile int n4 = 0;  // volatile objectconst struct {int n1;mutable int n2;} x = { 0, 0 };      // const object with mutable membern1 = 1; // ok, modifiable object//n2 = 2; // error: non-modifiable objectn4 = 3; // ok, treated as a side-effect//x.n1 = 4; // error: member of a const object is constx.n2 = 4; // ok, mutable member of a const object isn't constconst int& r1 = n1; // reference to const bound to non-const object//r1 = 2; // error: attempt to modify through reference to constconst_cast<int&>(r1) = 2; // ok, modifies non-const object n1fprintf(stdout, "n1: %d\n", n1); // 2const int& r2 = n2; // reference to const bound to const object//r2 = 2; // error: attempt to modify through reference to constconst_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); // 0return 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 togetherint 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, 100return 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 lambdafprintf(stdout, "x: %d\n", x); // 0return 0;
}} // namespace volatile_mutable_

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

總結

以上是生活随笔為你收集整理的C++中关键字volatile和mutable用法的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。