当前位置:网站首页>Volatile qualifier
Volatile qualifier
2022-06-25 21:14:00 【Rabbit cloud program】
Let's start with two questions
C/C++ Should I use in multithreaded programming volatile?
Some people think that volatile There is a great risk in solving the multi thread contention problem , Unless the environmental system used is unreliable, it will be added for insurance volatile, Or to realize the very low-level interface from the perspective of extreme efficiency . This requires the writer to be very clear about the direction of program logic , Otherwise you'll make a mistake . Isn't that amazing , I believe many people may not have written this keyword , But it's probably no stranger to experienced programmers .
C++11 The standard clearly points out that atomic operation or mutex lock should be used to solve the problem of multi-threaded data competition .
C and C++ Medium volatile It is not used to solve the problem of multi thread contention , It is used to modify some variables that change due to uncontrollable factors of the program , For example, accessing variables of underlying hardware devices , To remind the compiler not to optimize the access to this variable without authorization .
volatile Definition
keyword volatile Is an instruction to the compiler , Point out that such an object should not be optimized . stay C,C++,Java Inside , Once controversial , Later, with the memory model and multithreading , High concurrency scenarios , Bring him back to life .
volatile It's a type modifier (type modifier). It is designed to modify variables accessed and modified by different threads . without volatile, Basically, it will lead to such a result : Or you can't write multithreaded programs , Or the compiler loses a lot of optimization opportunities . Of course, single thread can only limit compiler optimization .
Application scenarios
Using and const Qualifiers are used in the same way volatile qualifiers .volatile A qualifier is an additional modifier to a type . If you're a rookie , You think of it as const Modifier that would be good .
Embedded aspect , use volatile The modified variable means that the variable may be changed by hardware , Such as register operation .
For example, programs that directly process hardware often have such data members , Their values are controlled by processes other than those controlled by the program itself . for example , The program can contain variables updated by the system clock . When the value of an object can be changed in a way other than compiler control or detection , The object should be lived as volatile.

边栏推荐
- Bank digital transformation layout in the beginning of the year, 6 challenges faced by financial level structure and Countermeasures
- OLED driven learning based on ssd1306 (II): addressing mode of ssd1306
- Yunzhisheng atlas supercomputing platform: computing acceleration practice based on fluid + alluxio (Part I)
- VFS appears when mounting a file system from an SD card: cannot open root device "mmcblk1p2“
- [nailing scenario capability package] smart office phone
- What is machine learning? (Fundamentals)
- Cvpr2019 | the latest cvpr2019 papers are the first to read!
- Basic process of configuring utf8 in idea
- New generation engineers teach you how to play with alluxio + ml (Part 2)
- Docker failed to remotely access 3306 after installing MySQL
猜你喜欢
![[nailing scenario capability package] manage the on-the-job / off-the-job situation of employees](/img/ec/c2f342a54ab69d8b834a8a1c8f8a01.jpg)
[nailing scenario capability package] manage the on-the-job / off-the-job situation of employees
Yanjiehua, editor in chief of Business Review: how to view the management trend of business in the future?
Chrome plugin installation
![[nailing scenario capability package] ranking of enterprise employees' points](/img/f5/ed5bd90706ef166c3758bc03ab32bc.jpg)
[nailing scenario capability package] ranking of enterprise employees' points

Yolov4 improved version comes out! Yolov4 extended edition! Yolov4 launched scaled-yolov4: scaling cross stage partial network
Yunzhisheng atlas supercomputing platform: computing acceleration practice based on fluid + alluxio (Part I)

Compile 6relayd using the cross compiler
Cloud development practice of the small program for brushing questions in the postgraduate entrance examination - page design and production (home page of the question bank, ranking page, my)

04 disk space management
![[nailing scenario capability package] company / Park Digital canteen](/img/72/eb3df1945532c4e7813e15f9cf90c5.jpg)
[nailing scenario capability package] company / Park Digital canteen
随机推荐
How to buy the millions of medical insurance for children? How much is it a year? Which product is the best?
Yunzhisheng atlas supercomputing platform: computing acceleration practice based on fluid + alluxio (Part I)
Bank digital transformation layout in the beginning of the year, 6 challenges faced by financial level structure and Countermeasures
Svn various color states
Code program related problems troubleshooting directory
Lesson 4 beautifulsoup
2022 oceanbase technical essay contest officially opened | come and release your force
109 practical shell scripts
[nailing scenario capability package] company / Park Digital canteen
[nailing scenario capability package] exhibition admission
Feature Engineering in simple terms – a practice guide based on openmldb (Part 1)
Basic knowledge of software engineering required for soft test
What is a subnet mask? (Powercert animated videos)
What is an app circle of friends advertisement
Tencent music knowledge map search practice
Patrol script
Record some questions about MySQL (DNS reverse resolution in Linux)
After 20 days' interview, I finally joined Ali (share the interview process)
[nail scenario capability package] hospital visitor verification
Compile 6relayd using the cross compiler