# Concurrency of Software Timers with loop()

**URL:** <https://community.particle.io/t/concurrency-of-software-timers-with-loop/27060>\
**Category:** Firmware\
**Created:** [November 6, 2016, 2:23am UTC](https://community.particle.io/t/concurrency-of-software-timers-with-loop/27060 "2016-11-06T02:23:50Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Eric24](https://sea2.discourse-cdn.com/flex026/user_avatar/community.particle.io/eric24/32/9031_2.png) [@Eric24](https://community.particle.io/u/Eric24)\
**Post date:** [November 6, 2016, 2:23am UTC](https://community.particle.io/t/concurrency-of-software-timers-with-loop/27060/1 "2016-11-06T02:23:50Z")

</div>

I couldn’t find any information on this topic, so please point me to it if I missed it.

The basic question is whether a software timer will ever interfere with code in the main loop. In other words, let’s say I have some global variables that are updated by code in a software timer, and periodically I also update those same global variables in the main loop. Will these two blocks of code ever step on each other? And further, will the _entire_ software timer function complete (or for that matter, will the entire main loop complete) “atomically”? I’m used to using mutexes or semaphores to coordinate “safe” access to global variables, but is that necessary with software timers?

_UPDATE: After a little more digging, I came across the ATOMIC\_BLOCK() function. I’m running in ‘threaded’ mode, so I add this around the two places that update the global variables. It works (although it did work before). Any thoughts on this approach?_

---

<div class="post-metadata">

**Author:** ![ScruffR](https://sea2.discourse-cdn.com/flex026/user_avatar/community.particle.io/scruffr/32/6952_2.png) [@ScruffR](https://community.particle.io/u/ScruffR)\
**Post date:** [November 6, 2016, 7:04am UTC](https://community.particle.io/t/concurrency-of-software-timers-with-loop/27060/2 "2016-11-06T07:04:50Z")

</div>

You might want to use `SINGLE_THREADED_BLOCK()` instead to still allow interrupts to get serviced.  
The Software Timers are running on a FreeRTOS thread alongside the application and the system thread with a 1ms time slice for timers and app (not sure about system).  
So single variable manipulations will not pose a problem since the code doesn’t actually run parallel. But if you are doing a lot of work on one variable (multi step calculations or extensive string building) a thread switch might occure.

Here is also some info  
[[SOLVED] Software Timer .isActive() returns false even when enabled](https://community.particle.io/t/solved-software-timer-isactive-returns-false-even-when-enabled/26650/13)
