Purpose of while(1); statement in C

前端 未结 12 1650
终归单人心
终归单人心 2020-12-08 00:53

What purpose does while(1); serve ? I am aware while(1) (no semicolon) loops infinitely and is similar to a spinlock situation. However I do not

12条回答
  •  旧时难觅i
    2020-12-08 01:12

    An usage on embedded software is to implement a software reset using the watchdog:

    while (1);
    

    or equivalent but safer as it makes the intent more clear:

    do { /* nothing, let's the dog bite */ } while (1);
    

    If the watchdog is enabled and is not acknowledged after x milliseconds we know it will reset the processor so use this to implement a software reset.

提交回复
热议问题