Click here to Skip to main content
15,886,963 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
AnswerRe: volatile misbehaves Pin
leon de boer18-May-18 8:16
leon de boer18-May-18 8:16 
GeneralRe: volatile misbehaves Pin
Vaclav_19-May-18 14:02
Vaclav_19-May-18 14:02 
GeneralRe: volatile misbehaves Pin
Vaclav_19-May-18 14:36
Vaclav_19-May-18 14:36 
GeneralRe: volatile misbehaves Pin
leon de boer20-May-18 4:41
leon de boer20-May-18 4:41 
GeneralRe: volatile misbehaves Pin
Vaclav_20-May-18 5:44
Vaclav_20-May-18 5:44 
GeneralRe: volatile misbehaves Pin
leon de boer20-May-18 20:35
leon de boer20-May-18 20:35 
GeneralRe: volatile misbehaves Pin
Vaclav_21-May-18 3:18
Vaclav_21-May-18 3:18 
GeneralRe: volatile misbehaves Pin
leon de boer21-May-18 4:33
leon de boer21-May-18 4:33 
Sometimes you want to read a status continually in a loop lets show you a sample
#include <stdint.h>
#define STATUS_PORT 0x3F000000;
int main (void){
    uint32_t* p = (uint32_t*) STATUS_PORT;
    do {} while (*p != 1); 
}

Now don't worry about the address the code will fail if you have an optimizer turned on ... it will produce this
main:
  mov r3, #1056964608
  ldr r3, [r3]
  cmp r3, #1
  beq .L2
.L3:
  b .L3
.L2:
  mov r0, #0
  bx lr

Hopefully you see the problem with the branch L3 loop its a deadloop doing nothing ... so you may ask why does it do it.

Well the code read the port at line 2 with ldr r3, [r3]
As far as the optimizer is concerned the value never changes so it thinks you are just asking for a deadloop with the while loop.
The optimizer has no way to know the port value can change independent of the running code.

What the volatile does is tell the optimizer the value can change without it knowing so it must read it everytime so lets do this
#include <stdint.h>
#define STATUS_PORT 0x3F000000;
int main (void){
    volatile uint32_t* p = (uint32_t*) STATUS_PORT; // We Add volatile to the pointer
    do {} while (*p != 1); 
}

Now what you get is this
main:
  mov r2, #1056964608
.L2:
  ldr r3, [r2]
  cmp r3, #1
  bne .L2
  mov r0, #0
  bx lr

See the difference in the L2 loop it reads the port everytime

So what the volatile is protecting you from is the optimizer making bad assumptions that the registers can't change.

That is all it's doing it makes sure that the optimizer knows the register value can change without the optimizer knowing.

Now it doesn't have to be a single register so long as you use the pointer anything at the pointer is volatile so consider this
#include <stdint.h>
#define STATUS_PORT 0x3F000000;

struct mystruct {
	uint32_t port1;
    uint32_t port2;
};

int main(void) {
	struct mystruct* p = (struct mystruct*)STATUS_PORT;
	do {} while (p->port1 != 1);
	do {} while (p->port2 != 2);
}

Again it gets it wrong on both ports ... see L3 and L5 .. deadloops again doing nothing
main:
  mov r3, #1056964608
  ldr r2, [r3]
  cmp r2, #1
  beq .L2
.L3:
  b .L3
.L2:
  ldr r3, [r3, #4]
  cmp r3, #2
  beq .L4
.L5:
  b .L5
.L4:
  mov r0, #0
  bx lr

Now put a volatile on the pointer
#include <stdint.h>
#define STATUS_PORT 0x3F000000;

struct mystruct {
	uint32_t port1;
    uint32_t port2;
};

int main(void) {
	volatile struct mystruct* p = (struct mystruct*)STATUS_PORT; // We add volatile to the pointer again
	do {} while (p->port1 != 1);
	do {} while (p->port2 != 2);
}

The compiler knows the ports pointed to by the pointer can change and produces the right code
main:
  mov r2, #1056964608
.L2:
  ldr r3, [r2]
  cmp r3, #1
  bne .L2
  mov r2, #1056964608
.L3:
  ldr r3, [r2, #4]
  cmp r3, #2
  bne .L3
  mov r0, #0
  bx lr

So there is your answer what the volatile does, its basically a thing to stop optimizer errors.
You only need it on things that can change outsize the running code like HARDWARE REGISTERS or data accessed by 2 processes of multitask code.
In vino veritas


modified 21-May-18 13:50pm.

Question__sync_synchronize stops processor ? Pin
Vaclav_16-May-18 10:31
Vaclav_16-May-18 10:31 
AnswerRe: __sync_synchronize stops processor ? Pin
Randor 16-May-18 12:15
professional Randor 16-May-18 12:15 
GeneralRe: __sync_synchronize stops processor ? Pin
Vaclav_16-May-18 13:15
Vaclav_16-May-18 13:15 
GeneralRe: __sync_synchronize stops processor ? Pin
Randor 16-May-18 14:10
professional Randor 16-May-18 14:10 
GeneralRe: __sync_synchronize stops processor ? Pin
Vaclav_17-May-18 3:22
Vaclav_17-May-18 3:22 
QuestionTo install Visual Studio(2008 Pro,2010 Express and 2013 Express) in the same machine Pin
manoharbalu14-May-18 19:30
manoharbalu14-May-18 19:30 
AnswerRe: To install Visual Studio(2008 Pro,2010 Express and 2013 Express) in the same machine Pin
Victor Nijegorodov14-May-18 20:24
Victor Nijegorodov14-May-18 20:24 
GeneralRe: To install Visual Studio(2008 Pro,2010 Express and 2013 Express) in the same machine Pin
manoharbalu14-May-18 20:50
manoharbalu14-May-18 20:50 
GeneralRe: To install Visual Studio(2008 Pro,2010 Express and 2013 Express) in the same machine Pin
Victor Nijegorodov14-May-18 22:56
Victor Nijegorodov14-May-18 22:56 
GeneralRe: To install Visual Studio(2008 Pro,2010 Express and 2013 Express) in the same machine Pin
manoharbalu15-May-18 3:10
manoharbalu15-May-18 3:10 
GeneralRe: To install Visual Studio(2008 Pro,2010 Express and 2013 Express) in the same machine Pin
Victor Nijegorodov15-May-18 3:40
Victor Nijegorodov15-May-18 3:40 
GeneralRe: To install Visual Studio(2008 Pro,2010 Express and 2013 Express) in the same machine Pin
manoharbalu15-May-18 3:48
manoharbalu15-May-18 3:48 
GeneralRe: To install Visual Studio(2008 Pro,2010 Express and 2013 Express) in the same machine Pin
leon de boer15-May-18 5:50
leon de boer15-May-18 5:50 
GeneralRe: To install Visual Studio(2008 Pro,2010 Express and 2013 Express) in the same machine Pin
Victor Nijegorodov15-May-18 9:57
Victor Nijegorodov15-May-18 9:57 
AnswerRe: To install Visual Studio(2008 Pro,2010 Express and 2013 Express) in the same machine Pin
Joe Woodbury17-May-18 5:55
professionalJoe Woodbury17-May-18 5:55 
QuestionCInternetSession OpenURL Pin
_Flaviu14-May-18 0:34
_Flaviu14-May-18 0:34 
AnswerRe: CInternetSession OpenURL Pin
Jochen Arndt14-May-18 2:01
professionalJochen Arndt14-May-18 2:01 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.