Would you like to inspect the original subtitles? These are the user uploaded subtitles that are being translated:
1
00:00:05,400 --> 00:00:09,060
Hello, and welcome. We're going to talk about another type qualifier in this lecture,
2
00:00:09,060 --> 00:00:12,060
mainly the volatile type qualifier.
3
00:00:12,060 --> 00:00:16,420
So this type qualifier tells the compiler explicitly that specified variable
4
00:00:16,420 --> 00:00:19,420
will change its value, so it's sort of the opposite of constant.
5
00:00:19,420 --> 00:00:22,180
So it's provided so that a program can tell the compiler to
6
00:00:22,180 --> 00:00:24,180
suppress various kinds of optimizations.
7
00:00:24,780 --> 00:00:27,480
It prevents the compiler from optimizing away
8
00:00:27,480 --> 00:00:29,980
seemingly redundant assignments to a variable.
9
00:00:30,640 --> 00:00:33,640
So it prevents the compiler from repeated examination of a variable
10
00:00:33,640 --> 00:00:35,840
without its value seemingly changing.
11
00:00:36,540 --> 00:00:38,840
So it essentially prevents the compiler
12
00:00:38,840 --> 00:00:41,040
from caching variables.
13
00:00:41,540 --> 00:00:45,000
So the reason for having this type qualifier is mainly because of the problems
14
00:00:45,000 --> 00:00:49,000
that are encountered in real time or embedded systems programming.
15
00:00:49,000 --> 00:00:53,000
Programs that have a lot of threading, programs where the resources are scarce
16
00:00:53,000 --> 00:00:56,300
will use the volatile type qualifier.
17
00:00:57,200 --> 00:01:00,000
So some use cases of when you should use this.
18
00:01:00,000 --> 00:01:04,600
A variable should be declared volatile whenever its value could change unexpectedly.
19
00:01:05,400 --> 00:01:10,660
The optimizer must be careful to reload the variable every time it is used instead of holding a copy.
20
00:01:10,660 --> 00:01:14,540
You don't want the compiler to use caching.
21
00:01:14,540 --> 00:01:17,540
So there's only three types of variables that should use volatile:
22
00:01:18,040 --> 00:01:19,840
memory mapped profile registers,
23
00:01:19,840 --> 00:01:23,500
global variables non-stack variables modified by interrupting
24
00:01:23,500 --> 00:01:27,160
service routine and global variables accessed by multiple tax
25
00:01:27,160 --> 00:01:29,160
within a multi-threaded application.
26
00:01:29,660 --> 00:01:34,060
And usually, that's when you'll use it you'll use it when you're doing a lot of threading.
27
00:01:34,060 --> 00:01:38,460
So the syntax, it's basically the same as it is for other type qualifiers.
28
00:01:38,460 --> 00:01:40,860
You basically have to say volatile before the data type.
29
00:01:41,360 --> 00:01:44,160
And this will say location one for example
30
00:01:44,160 --> 00:01:45,660
is a volatile location.
31
00:01:46,260 --> 00:01:49,760
Volatile int star ploc that points to a volatile location pointer right.
32
00:01:50,760 --> 00:01:55,360
So loc1 is a volatile value ploc points to a volatile value.
33
00:01:55,360 --> 00:01:56,860
So let's look at an example.
34
00:01:57,760 --> 00:02:02,960
So here we have val 1 equals x. And then we have some code, it's not using x.
35
00:02:02,960 --> 00:02:05,160
And then we have val2 equals x.
36
00:02:05,160 --> 00:02:09,520
So this is just basically making assignments, and then in between those assignments,
37
00:02:09,520 --> 00:02:13,120
you're doing all this other code and you're not accessing variable x.
38
00:02:13,780 --> 00:02:16,480
A smart optimizing compiler might notice
39
00:02:16,480 --> 00:02:19,880
that you only use x twice without changing its value.
40
00:02:19,880 --> 00:02:24,080
So normally what the compiler do is it'll temporarily store the x value in a register
41
00:02:24,680 --> 00:02:26,880
because it's not changing a lot, it will kind of cache it.
42
00:02:27,380 --> 00:02:29,980
So when x is needed for val 2,
43
00:02:29,980 --> 00:02:33,340
it can save time by just reading the value from the register instead of
44
00:02:33,340 --> 00:02:38,000
from the original memory location. We know from the register keyword that registers
45
00:02:38,000 --> 00:02:40,560
and storing things in there is much faster.
46
00:02:40,560 --> 00:02:44,560
So sometimes you don't want this optimization, you don't want to use this value from the register.
47
00:02:45,260 --> 00:02:48,760
So specifically it's not going to be desired if x is changed between
48
00:02:48,760 --> 00:02:50,760
two statements by some other agency.
49
00:02:51,260 --> 00:02:56,250
So you would use the volatile keyword to ensure that the compiler does not optimize this value away
50
00:02:56,250 --> 00:02:59,250
and instead has a stored value for each variable.
51
00:02:59,250 --> 00:03:03,050
You do not want to do this optimization because somewhere in here
52
00:03:03,050 --> 00:03:06,050
maybe a thread somewhere is accessing this data
53
00:03:06,050 --> 00:03:09,050
and you don't want to cache it because then it won't
54
00:03:09,050 --> 00:03:12,710
it won't actually if you modify it it's not going to be reflected in val2.
55
00:03:13,910 --> 00:03:15,910
So if the volatile keyword is not using declaration,
56
00:03:15,910 --> 00:03:19,570
the compiler can assume that a value has not changed between these cases
57
00:03:19,570 --> 00:03:24,560
and it can then attempt to optimize the code and we don't want that, specifically for threading.
58
00:03:24,920 --> 00:03:27,620
Let's look at another example with an i/o port.
59
00:03:27,620 --> 00:03:32,280
Suppose you have an output port. It's pointed to by a variable in your program.
60
00:03:32,640 --> 00:03:36,640
So if you wanted to write two characters to the port: an o followed by an n,
61
00:03:36,640 --> 00:03:41,300
you might say star output point equals o, star outport equals n.,
62
00:03:41,300 --> 00:03:46,000
very simple assignments dereferencing and direction operator assigning characters.
63
00:03:46,000 --> 00:03:49,660
A smart compiler might notice two successive assignments in the same location
64
00:03:49,660 --> 00:03:53,860
because outport is not being modified in between the compiler would remove the first assignment
65
00:03:53,860 --> 00:03:54,860
from the program.
66
00:03:54,860 --> 00:03:58,760
So to prevent this from happening, you can declare outPort to be volatile
67
00:03:58,760 --> 00:04:02,750
a volatile pointer right. You just say volatile char star outport
68
00:04:02,750 --> 00:04:04,750
and you don't have this optimization happen.
69
00:04:04,750 --> 00:04:06,750
You can also use volatile with constant.
70
00:04:07,410 --> 00:04:09,410
It can be both constant and volatile.
71
00:04:09,410 --> 00:04:12,770
A hardware clock setting normally should not be changed by the program.
72
00:04:12,770 --> 00:04:14,270
You can make it constant,
73
00:04:14,270 --> 00:04:17,270
however, it is changed by an agency other than the program
74
00:04:17,270 --> 00:04:20,630
and an agency just means another process or another thread.
75
00:04:20,630 --> 00:04:25,290
So you might want to make it volatile, so you don't do that caching and you don't do that optimization
76
00:04:25,290 --> 00:04:26,890
because it will screw things up.
77
00:04:26,890 --> 00:04:30,890
So you can use both qualifiers and declaration and the order does not matter.
78
00:04:30,890 --> 00:04:34,890
Volatile constant int location or constant volatile int
79
00:04:34,890 --> 00:04:38,550
star p location. And that's the gist between volatile.
80
00:04:38,550 --> 00:04:43,510
You'll see it used more again with threading. So when we talk about threads in a later section,
81
00:04:43,510 --> 00:04:46,060
you're going to get good taste of this keyword.
82
00:04:46,060 --> 00:04:49,260
Most times you don't have to worry about it, but again it's really just an optimization,
83
00:04:49,260 --> 00:04:52,620
telling the compiler do not optimize my value away
84
00:04:52,620 --> 00:04:55,280
to be put into a register so that it's fast,
85
00:04:55,280 --> 00:04:58,880
make sure that i get the most up-to-date value. Thank you.
8398
Can't find what you're looking for?
Get subtitles in any language from opensubtitles.com, and translate them here.