SlideShare una empresa de Scribd logo
1 de 2
Descargar para leer sin conexión
volatile 的作用
volatile 的本意是“易变的”

由于访问寄存器的速度要快过 RAM,所以编译器一般都会作减少存取外部 RAM 的优化。比如:

static int i=0;

int main(void)
{
...
while (1)
{
if (i) dosomething();
}
}

/* Interrupt service routine. */
void ISR_2(void)
{
i=1;
}

程序的本意是希望 ISR_2 中断产生时,在 main 当中调用 dosomething 函数,但是,由于编译器判断在
main 函数里面没有修改过 i,因此
可能只执行一次对从 i 到某寄存器的读操作,然后每次 if 判断都只使用这个寄存器里面的 “i 副本 ”,导致
dosomething 永远也不会被
调用。如果将将变量加上 volatile 修饰,则编译器保证对此变量的读写操作都不会被优化(肯定执行)。此例中 i
也应该如此说明。

一般说来,volatile 用在如下的几个地方:

1、中断服务程序中修改的供其它程序检测的变量需要加 volatile;

2、多任务环境下各任务间共享的标志应该加 volatile;(当一个线程或者进程修改了之后,在另一个线
程或进程中必须读取的是更新后的数据[volatile 强制读取内存])。

3、存储器映射的硬件寄存器通常也要加 volatile 说明,因为每次对它的读写都可能由不同意义;

另外,以上这几种情况经常还要同时考虑数据的完整性(相互关联的几个标志读了一半被打断了重写),
在 1 中可以通过关中断来实
现,2 中可以禁止任务调度,3 中则只能依靠硬件的良好设计了。




volatile 的含义
volatile 总是与优化有关,编译器有一种技术叫做数据流分析,分析程序中的变量在哪里赋值、在哪里使用、在
哪里失效,分析结果可以用于常量合并,常量传播等优化,进一步可以死代码消除。但有时这些优化不是程序所
需要的,这时可以用 volatile 关键字禁止做 这些优化,volatile 的字面含义是易变的,它有下面的作用:

1 不会在两个操作之间把 volatile 变量缓存在寄存器中。在多任务、中断、甚至 setjmp 环境下,变量可能被其他
的程序改变,编译器 自己无法知道,volatile 就是告诉编译器这种情况。
2 不做常量合并、常量传播等优化,所以像下面的代码:
volatile int i = 1;
if (i > 0) ...

if 的条件不会当作无条件真。

3 对 volatile 变量的读写不会被优化掉。如果你对一个变量赋值但后面没用到,编译器常常可以省略那
个赋值操作,然而对 Memory Mapped IO 的处理是不能这样优化的。

前面有人说 volatile 可以保证对内存操作的原子性,这种说法不大准确,其一,x86 需要 LOCK 前缀才能在 SMP
下保证原子性,其二,RISC 根本不能对内存直接运算,要保证原子性得用别的方法,如 atomic_inc。

对于 jiffies,它已经声明为 volatile 变量,我认为直接用 jiffies++就可以了,没必要用那种复杂的形式,因为那
样也不能保证原子性。

你可能不知道在 Pentium 及后续 CPU 中,下面两组指令

inc jiffies
;;
mov jiffies, %eax
inc %eax
mov %eax, jiffies

作用相同,但一条指令反而不如三条指令快。

Más contenido relacionado

Destacado

2024 State of Marketing Report – by Hubspot
2024 State of Marketing Report – by Hubspot2024 State of Marketing Report – by Hubspot
2024 State of Marketing Report – by HubspotMarius Sescu
 
Everything You Need To Know About ChatGPT
Everything You Need To Know About ChatGPTEverything You Need To Know About ChatGPT
Everything You Need To Know About ChatGPTExpeed Software
 
Product Design Trends in 2024 | Teenage Engineerings
Product Design Trends in 2024 | Teenage EngineeringsProduct Design Trends in 2024 | Teenage Engineerings
Product Design Trends in 2024 | Teenage EngineeringsPixeldarts
 
How Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental HealthHow Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental HealthThinkNow
 
AI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdfAI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdfmarketingartwork
 
PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024Neil Kimberley
 
Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)contently
 
How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024Albert Qian
 
Social Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsSocial Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsKurio // The Social Media Age(ncy)
 
Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024Search Engine Journal
 
5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summarySpeakerHub
 
ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd Clark Boyd
 
Getting into the tech field. what next
Getting into the tech field. what next Getting into the tech field. what next
Getting into the tech field. what next Tessa Mero
 
Google's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search IntentGoogle's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search IntentLily Ray
 
Time Management & Productivity - Best Practices
Time Management & Productivity -  Best PracticesTime Management & Productivity -  Best Practices
Time Management & Productivity - Best PracticesVit Horky
 
The six step guide to practical project management
The six step guide to practical project managementThe six step guide to practical project management
The six step guide to practical project managementMindGenius
 
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...RachelPearson36
 

Destacado (20)

2024 State of Marketing Report – by Hubspot
2024 State of Marketing Report – by Hubspot2024 State of Marketing Report – by Hubspot
2024 State of Marketing Report – by Hubspot
 
Everything You Need To Know About ChatGPT
Everything You Need To Know About ChatGPTEverything You Need To Know About ChatGPT
Everything You Need To Know About ChatGPT
 
Product Design Trends in 2024 | Teenage Engineerings
Product Design Trends in 2024 | Teenage EngineeringsProduct Design Trends in 2024 | Teenage Engineerings
Product Design Trends in 2024 | Teenage Engineerings
 
How Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental HealthHow Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental Health
 
AI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdfAI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdf
 
Skeleton Culture Code
Skeleton Culture CodeSkeleton Culture Code
Skeleton Culture Code
 
PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024
 
Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)
 
How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024
 
Social Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsSocial Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie Insights
 
Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024
 
5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary
 
ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd
 
Getting into the tech field. what next
Getting into the tech field. what next Getting into the tech field. what next
Getting into the tech field. what next
 
Google's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search IntentGoogle's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search Intent
 
How to have difficult conversations
How to have difficult conversations How to have difficult conversations
How to have difficult conversations
 
Introduction to Data Science
Introduction to Data ScienceIntroduction to Data Science
Introduction to Data Science
 
Time Management & Productivity - Best Practices
Time Management & Productivity -  Best PracticesTime Management & Productivity -  Best Practices
Time Management & Productivity - Best Practices
 
The six step guide to practical project management
The six step guide to practical project managementThe six step guide to practical project management
The six step guide to practical project management
 
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
 

volatile的作用

  • 1. volatile 的作用 volatile 的本意是“易变的” 由于访问寄存器的速度要快过 RAM,所以编译器一般都会作减少存取外部 RAM 的优化。比如: static int i=0; int main(void) { ... while (1) { if (i) dosomething(); } } /* Interrupt service routine. */ void ISR_2(void) { i=1; } 程序的本意是希望 ISR_2 中断产生时,在 main 当中调用 dosomething 函数,但是,由于编译器判断在 main 函数里面没有修改过 i,因此 可能只执行一次对从 i 到某寄存器的读操作,然后每次 if 判断都只使用这个寄存器里面的 “i 副本 ”,导致 dosomething 永远也不会被 调用。如果将将变量加上 volatile 修饰,则编译器保证对此变量的读写操作都不会被优化(肯定执行)。此例中 i 也应该如此说明。 一般说来,volatile 用在如下的几个地方: 1、中断服务程序中修改的供其它程序检测的变量需要加 volatile; 2、多任务环境下各任务间共享的标志应该加 volatile;(当一个线程或者进程修改了之后,在另一个线 程或进程中必须读取的是更新后的数据[volatile 强制读取内存])。 3、存储器映射的硬件寄存器通常也要加 volatile 说明,因为每次对它的读写都可能由不同意义; 另外,以上这几种情况经常还要同时考虑数据的完整性(相互关联的几个标志读了一半被打断了重写), 在 1 中可以通过关中断来实 现,2 中可以禁止任务调度,3 中则只能依靠硬件的良好设计了。 volatile 的含义 volatile 总是与优化有关,编译器有一种技术叫做数据流分析,分析程序中的变量在哪里赋值、在哪里使用、在 哪里失效,分析结果可以用于常量合并,常量传播等优化,进一步可以死代码消除。但有时这些优化不是程序所 需要的,这时可以用 volatile 关键字禁止做 这些优化,volatile 的字面含义是易变的,它有下面的作用: 1 不会在两个操作之间把 volatile 变量缓存在寄存器中。在多任务、中断、甚至 setjmp 环境下,变量可能被其他 的程序改变,编译器 自己无法知道,volatile 就是告诉编译器这种情况。
  • 2. 2 不做常量合并、常量传播等优化,所以像下面的代码: volatile int i = 1; if (i > 0) ... if 的条件不会当作无条件真。 3 对 volatile 变量的读写不会被优化掉。如果你对一个变量赋值但后面没用到,编译器常常可以省略那 个赋值操作,然而对 Memory Mapped IO 的处理是不能这样优化的。 前面有人说 volatile 可以保证对内存操作的原子性,这种说法不大准确,其一,x86 需要 LOCK 前缀才能在 SMP 下保证原子性,其二,RISC 根本不能对内存直接运算,要保证原子性得用别的方法,如 atomic_inc。 对于 jiffies,它已经声明为 volatile 变量,我认为直接用 jiffies++就可以了,没必要用那种复杂的形式,因为那 样也不能保证原子性。 你可能不知道在 Pentium 及后续 CPU 中,下面两组指令 inc jiffies ;; mov jiffies, %eax inc %eax mov %eax, jiffies 作用相同,但一条指令反而不如三条指令快。