当前位置:网站首页>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.
边栏推荐
- What is ARP (address resolution protocol)? (Powercert animated videos)
- Soft test intermediate simple notes
- [phase 23] phased summary of spring recruitment practice (Alibaba cloud has OC)
- What is a subnet mask? (Powercert animated videos)
- couldn‘t be accessed by user ‘_ apt‘
- Openocd adds third-party device support: ht32f52352 Cortex-M0+
- The beginning of manjaro's journey
- [nailing scenario capability package] exhibition admission
- Install JDK, MySQL and nexus under Linux (tar package installation)
- Node installation method you don't know
猜你喜欢
Alicloud disk mounted locally
How to play one to many in JPA?
Explain memcached principle in detail
Installing mysql8 under centos8
[nailing scenario capability package] ranking of enterprise employees' points
What is a subnet mask? (Powercert animated videos)
New generation engineers teach you how to play with alluxio + ml (Part 2)
Literals and type conversions of basic data types
Chrome plugin installation
Flexible scale out: from file system to distributed file system
随机推荐
Introduction to the basics of kotlin language: lambda expression
ZABBIX foundation details
Getting started and using postman
Molecular dynamics - basic characteristics of molecular force field
Inventory - those automated test interview questions you can't help but know
How to buy the millions of medical insurance for children? How much is it a year? Which product is the best?
Interview shock: talk about thread life cycle and transformation process?
Is Galaxy Securities reliable? Is it safe to open a securities account?
Causes and solutions of unreliable JS timer execution
UDP Vs TCP (Powercert animated videos)
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)
Idea implements hot deployment
HMS core actively explores the function based on hardware ear return, helping to reduce the overall singing delay rate of the singing bar by 60%
2022年启牛学堂证券开户安全嘛?
COMP9024
How does zhiting home cloud and home assistant access homekit respectively? What is the difference between them?
How to close gracefully after using jedis
Lesson 3 urllib
[opencv] opencv from introduction to mastery -- detailed explanation of input and output XML and yaml files
What is a server? (Powercert animated videos)