真实的国产乱ⅩXXX66竹夫人,五月香六月婷婷激情综合,亚洲日本VA一区二区三区,亚洲精品一区二区三区麻豆

成都創(chuàng)新互聯(lián)網(wǎng)站制作重慶分公司

C++中mutable與volatile的深入理解

前言

成都創(chuàng)新互聯(lián)致力于互聯(lián)網(wǎng)網(wǎng)站建設(shè)與網(wǎng)站營銷,提供成都網(wǎng)站設(shè)計(jì)、網(wǎng)站制作、網(wǎng)站開發(fā)、seo優(yōu)化、網(wǎng)站排名、互聯(lián)網(wǎng)營銷、重慶小程序開發(fā)、公眾號商城、等建站開發(fā),成都創(chuàng)新互聯(lián)網(wǎng)站建設(shè)策劃專家,為不同類型的客戶提供良好的互聯(lián)網(wǎng)應(yīng)用定制解決方案,幫助客戶在新的全球化互聯(lián)網(wǎng)環(huán)境中保持優(yōu)勢。

C++中修飾數(shù)據(jù)可變的關(guān)鍵字有三個(gè):const、volatile和mutable。const比較好理解,表示其修飾的內(nèi)容不可改變(至少編譯期不可改變),而volatile和mutable恰好相反,指示數(shù)據(jù)總是可變的。mutable和volatile均可以和const搭配使用,但兩者在使用上有比較大差別。

下面話不多說了,來一起看看詳細(xì)的介紹吧

mutable

mutable只能作用在類成員上,指示其數(shù)據(jù)總是可變的。不能和const 同時(shí)修飾一個(gè)成員,但能配合使用:const修飾的方法中,mutable修飾的成員數(shù)據(jù)可以發(fā)生改變,除此之外不應(yīng)該對類/對象帶來副作用。

考慮一個(gè)mutable的使用場景:呼叫系統(tǒng)中存有司機(jī)(Driver)的信息,為了保護(hù)司機(jī)的隱私,司機(jī)對外展現(xiàn)的聯(lián)系號碼每隔五分鐘從空閑號碼池更新一次。根據(jù)需求,Driver類的實(shí)現(xiàn)如下偽代碼:

class Driver {
private:
...
// real phone number
string phone;
// display phone number
mutable string displayPhone;

public:
string getDisplayPhone() const {
if (needUpdate()) {
lock.lock();
if (needUpdate()) {
updateDisplayPhone(); // displayPhone在這里被改變
}
lock.unlock();
}
return displayPhone;
}
};

在上述代碼中,const方法中不允許對常規(guī)成員進(jìn)行變動,但mutable成員不受此限制。對Driver類來說,其固有屬性(姓名、年齡、真實(shí)手機(jī)號等)未發(fā)生改變,符合const修飾。mutable讓一些隨時(shí)可變的展示屬性能發(fā)生改變,達(dá)到了靈活編程的目的。

volatile

volatile用于修飾成員或變量,指示其修飾對象可能隨時(shí)變化,編譯器不要對所修飾變量進(jìn)行優(yōu)化(緩存),每次取值應(yīng)該直接讀取內(nèi)存。由于volatile的變化來自運(yùn)行期,其可以與const一起使用。兩者一起使用可能讓人費(fèi)解,如果考慮場景就容易許多:CPU和GPU通過映射公用內(nèi)存中的同一塊,GPU可能隨時(shí)往共享內(nèi)存中寫數(shù)據(jù)。對CPU上的程序來說,const修飾變量一直是右值,所以編譯通過。但其變量內(nèi)存中的值在運(yùn)行期間可能隨時(shí)在改變,volatile修飾是正確做法。

在多線程環(huán)境下,volatile可用作內(nèi)存同步手段。例如多線程爆破密碼:

volatile bool found = false;

void run(string target) {
while (!found) {
// 計(jì)算字典口令的哈希
if (target == hash) {
found = true;
break;
}
}
}

在volatile的修飾下,每次循環(huán)都會檢查內(nèi)存中的值,達(dá)到同步的效果。

需要注意的是,volatile的值可能隨時(shí)會變,期間會導(dǎo)致非預(yù)期的結(jié)果。例如下面的例子求平方和:

double square(volatile double a, volatile double b) {
return (a + b) * (a + b);
}

a和b都是隨時(shí)可變的,所以上述代碼中的第一個(gè)a + b可能和第二個(gè)不同,導(dǎo)致出現(xiàn)非預(yù)期的結(jié)果。這種情況下,正確做法是將值賦予常規(guī)變量,然后再相乘:

double square(volatile double a, volatile double b) {
double c = a + b;
return c * c;
}

一般說來,volatile用在如下的幾個(gè)地方:

1. 中斷服務(wù)程序中修改的供其它程序檢測的變量需要加volatile;

2. 多任務(wù)環(huán)境下各任務(wù)間共享的標(biāo)志應(yīng)該加volatile;

3. 存儲器映射的硬件寄存器通常也要加volatile說明,因?yàn)槊看螌λ淖x寫都可能有不同意義;

總結(jié)

mutable只能用與類變量,不能與const同時(shí)使用;在const修飾的方法中,mutable變量數(shù)值可以發(fā)生改變;
volatile只是運(yùn)行期變量的值隨時(shí)可能改變,這種改變即可能來自其他線程,也可能來自外部系統(tǒng)。

好了,以上就是這篇文章的全部內(nèi)容了,希望本文的內(nèi)容對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,如果有疑問大家可以留言交流,謝謝大家對創(chuàng)新互聯(lián)的支持。

參考

https://en.cppreference.com/w/cpp/language/cv

下面是其他網(wǎng)友的補(bǔ)充

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

(1). 編譯器會對帶有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è)變量且該變量的值會被改變時(shí)應(yīng)該用volatile聲明,該關(guān)鍵字的作用是防止編譯器優(yōu)化把變量從內(nèi)存裝入CPU寄存器中。如果變量被裝入寄存器,那么多個(gè)線程有可能有的使用內(nèi)存中的變量,有的使用寄存器中的變量,這會造成程序的錯誤執(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變量間的操作,是不會被編譯器交換順序的,能夠保證volatile變量間的順序性,編譯器不會進(jìn)行亂序優(yōu)化(The value cannot change in order of assignment)。但volatile變量和非volatile變量之間的順序,編譯器不保證順序,可能會進(jìn)行亂序優(yōu)化。

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

(1). 允許即使包含它的對象被聲明為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ì)用法見下面的測試代碼,下面是從其他文章中copy的測試代碼,詳細(xì)內(nèi)容介紹可以參考對應(yīng)的reference:

#include "volatile_mutable.hpp"
#include 
#include 
#include 
#include 
#include 
 
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(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(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 lk(m);
			return data;
		}
		void inc() {
			std::lock_guard 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


本文名稱:C++中mutable與volatile的深入理解
瀏覽地址:http://weahome.cn/article/jjpdpg.html

其他資訊

在線咨詢

微信咨詢

電話咨詢

028-86922220(工作日)

18980820575(7×24)

提交需求

返回頂部