Reputation: 3
I will first explain the situation :
The sudo code is as follows :
ISR_USB()
{
char command=read_from_buffer();
printf("Entered ISR and command = %c",command); // Prints on serial port and confirms the program entered ISR
if(command==STOP_DEMO)
FLAG_TO_BREAK_WHILE=true;
printf("%u",FLAG_TO_BREAK_WHILE); // Confirms correct value of flag is set
command_parser(command);
}
command_parser(command)
{
if(command=='1')
printf("HELLO WORLD");
else if(command=='2')
{
printf("While started");
while(!FLAG_TO_BREAK_WHILE); // Gets stuck here
/*
starts working if the above while is replaced by :
while(!FLAG_TO_BREAK_WHILE)
{
printf("%u",FLAG_TO_BREAK_WHILE);
}
*/
}
else if (command=='3')
printf("stop command executed");
}
please help me understand the situation here and this behavior.
NOTE: THE PARSER IS IN ANOTHER FILE AND THE VARIABLES ARE EXTERNED.
Upvotes: 0
Views: 797
Reputation: 520
What is happening is that while(!FLAG)
is being optimized to
if(!FLAG)
{
while(true)
{
//do stuff
}
}
To fix this define the flag as volatile and the compiler will be forced to read the flag from memory every time it is accessed by your code.
Upvotes: 5