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

Let's say we have firmware with:

SYSTEM_THREAD(ENABLED);

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?

Thanks.

1 Like

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”.

2 Likes

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.

2 Likes

thank you both!

1 Like