Cloud functions: are they thread safe in regards to loop()?

Let’s say we have firmware with:


If a cloud function modifies a global variable that we want to read from loop(), do we need to make such variable an atomic one to ensure proper handling?


Particle.function() callbacks are running on the user/application thread just like loop() so there is no need to guard the two against each other’s “interference”.


That is correct. Functions and variables are processed on the main loop thread, between calls to loop(), so there is no issue with thread safety if you are modifying values from loop as well.

In some old versions of Device OS, variables were not read from loop, which did lead to the case where random data could be returned, particularly with String variables, if you modified it when the system was reading it. This has been fixed since 1.5.0.

You would need to add thread safety if you were interacting with data from a worker thread or software timer.


thank you both!

1 Like