Machine-translated by Google. Questions are authored in English.
Top

Discussion

What is wrong with this attempt to wait for a flag set by a handler?

uint8_t ready = 0;
while (!ready) { }

  • A.the loop body is empty, which is not permitted in C
  • B.the flag should be signed rather than unsigned
  • C.the handler cannot write to a variable of that type
  • D.ready is not volatile, so the compiler may hoist the read

Answer: D

With optimisation on, the compiler reads the variable once, sees nothing in the loop that could change it and generates an infinite loop. Adding the qualifier forces a fresh read on every iteration, and a semaphore would be better still.

No comment is present. Be the first to comment.
Loading…

Post your comment

Please log in or register to post a comment.