Atomicstate - mutual exclusion?

Hi, I am a newbie started to learn more about writing smartapps. I came across the atomicstate as I want to keep track of smartapp states (

Based on the description of the atomicstate implementation, it only guarantee the read / write operations are of the most updated values across simultaneous execution of the same smartapp. A race condition can still exists between multiple read / write operations within the same instance of the smartapp. What I mean is:

Assume an atomicstate “a” with the following code sequence

atomicstate.a = 1;
if (atomicstate.a == 1) {
  /* this part may never be executed */

Am I correct?

Since any smartapp can subscribe to one or more events, serializing event execution can lead to deadlock and therefore is not possible. If I am correct, any plan on adding mutual exclusion or test & set operation to the smartapp? Or am I thinking too much? :wink:


Yep, you hit the nail on the head!

There are lots of fun / scary issues that arise in an asynchronous environment like this. Keeps you on your toes.

Best to avoid the circumstances you are describing.


While this is true, I wouldn’t loose much sleep over it.
Personally I’ve never had the need to use atomicState in a smart app, but hey, that’s just me, I guess my apps are too simple to need it.
Honestly, with cloud latency variations that exist any app that needs to fire off that frequently is likely to become unpredictable anyway.
state works just fine within all references of a running smartapp instance, and you can also pass data structures around as parameters to methods, and returns from methods.


I was thinking about this issue because I like to try combining all the smartapps that are now running with my door lock. Each of them deals with one door event and therefore, atomicity is not required (same event will not generated milliseconds apart from my door :smile:).

However, when I am trying to combined with other sensors and devices @ home to create a completed “scene” handling smartapps, I will need mutex to ensure some devices / sensors subscribed by my app wont change the state while my smartapp is trying to execute an event like “door locked using the keypad”…

Thanks for all your confirmation… Happy Holidays!