-
Notifications
You must be signed in to change notification settings - Fork 2
/
Volatile.txt
57 lines (53 loc) · 2.33 KB
/
Volatile.txt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
In C programming, the `volatile` keyword is used to indicate that a variable's value may change unexpectedly. This keyword informs the compiler that the variable's value should not be optimized, cached, or stored in registers because it can change outside of the program's control.
There are several practical examples where the use of the `volatile` keyword is necessary:
1. Interrupt Service Routines (ISRs):
When an interrupt occurs, an ISR is executed, which may modify variables shared between the main program and the ISR. The use of `volatile` is essential to ensure that the variables' values are not cached or optimized, and the program reads the latest value.
For example:
```
volatile int count;
void ISR()
{
count++;
//incrementing count
}
int main()
{
while(1)
{ printf("%d\n",count); //reading count
}
return 0;
}
```
2. Memory-mapped I/O:
In embedded systems, hardware devices are often mapped to memory locations, and C programs access these devices through memory-mapped I/O. Since the hardware can modify the values at any time, the `volatile` keyword must be used to ensure that the program reads the latest value.
For example:
```
volatile uint8_t* device = (uint8_t*) 0x1234; //device address
*device = 0x55; //write to device
uint8_t value = *device;
//read from device
```
3. Multithreaded Programming:
In multithreaded programming, several threads may access the same variable, and a thread's execution order cannot be guaranteed. The `volatile` keyword ensures that the variable is not optimized, cached, or stored in registers, so any thread can read the latest value.
For example:
```
volatile int shared_variable;
void* thread_func(void* arg)
{
shared_variable++; //incrementing shared variable
pthread_exit(NULL);
}
int main()
{
pthread_t thread;
pthread_create(&thread,NULL,thread_func,NULL);
//create thread
while(1)
{
printf("%d\n",shared_variable);
//reading shared variable
}
return 0;
}
```
In summary, the `volatile` keyword is used to inform the compiler that a variable's value can change unexpectedly, and its value should not be optimized or cached. The keyword is essential in scenarios like ISRs, memory-mapped I/O, and multithreaded programming to ensure the program reads the latest value of the variable.