I had the following code:
system_tick_t now = millis();
system_tick_t time_since_change = now - m_last_state_change;
system_tick_t time_since_update = now - m_last_current_temp;
...
if (time_since_update > (10 * 60 * 1000UL))...
...
Particle.subscribe(X, update);
...
void update(const char *event, const char *data) {
m_last_current_temp = millis();
}
and I noticed that the if
was firing even though updates were being received at regular intervals. After some testing it seems sometimes that period can go negative. I figure it must happen when an event is received between the first couple of lines.
I switched the code to:
system_tick_t now, time_since_change, time_since_update;
SINGLE_THREADED_BLOCK() {
now = millis();
time_since_change = now - m_last_state_change;
time_since_update = now - m_last_current_temp;
}
and so far haven’t seen the issue return.
This does make me suspect of other code I’ve used and seen used that does the same operation on one line. For example the following code if it was possible that a lastSync
was updated outside of the loop.
if (millis() - lastSync > ONE_DAY_MILLIS) {
Are these all not safe? and is SINGLE_THREADED_BLOCK
the correct way to make them safe?
If so I suppose I’d suggest an update to the subscribe
documentation that makes it clear that received messages can interrupt the normal program flow even if not using any special threading.
(if this also occurs outside of mesh
feel free to relocate this thread)