Skip to content

Millis() and micros() can’t be used for detecting timeouts #136

Open
@lasselukkari

Description

@lasselukkari

I believe one of the most common use cases for the time related functions millis() and micros() is to detect timeouts. In a nutshell you take the current time and loop until you succeed on something or bail out when you hit the timeout. An example of such a loop can be seen here:
https://github.com/arduino/ArduinoCore-avr/blob/master/cores/arduino/Stream.cpp#L31-L40

Many people use the same pattern in their libraries:
https://github.com/arduino-libraries/ArduinoHttpClient/blob/master/src/HttpClient.cpp#L414-L420

The problem is that the return value of millis() does not advance between calls if it’s not manually set. When calling a function that expects the clock to go forward it will never return because it is impossible to set a new value for the millis while the other function is being executed.

An easy fix would to increment the return values of millis() and micros() by one on every call. If needed this behaviour could be configurable.

Any ideas?

Metadata

Metadata

Assignees

No one assigned

    Labels

    arduino mocksCompilation mocks for the Arduino libraryenhancementNew feature or request

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions