我将首先解释一下情况:
sudo代码如下:
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");
}
请帮助我了解这里的情况和这种行为。
注意:PARSER在另一个文件中并且变量是异常的。
答案 0 :(得分:5)
正在发生的事情是while(!FLAG)
正在优化
if(!FLAG)
{
while(true)
{
//do stuff
}
}
要解决此问题,请将标志定义为volatile,并且每次代码访问时,编译器都将被强制从内存中读取标志。