# Are timers preemptive?

**URL:** <https://community.particle.io/t/are-timers-preemptive/38574>\
**Category:** Firmware\
**Created:** [January 8, 2018, 8:21pm UTC](https://community.particle.io/t/are-timers-preemptive/38574 "2018-01-08T20:21:26Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![AustinGlaser](https://avatars.discourse-cdn.com/v4/letter/a/ed655f/32.png) [@AustinGlaser](https://community.particle.io/u/AustinGlaser)\
**Post date:** [January 8, 2018, 8:21pm UTC](https://community.particle.io/t/are-timers-preemptive/38574/1 "2018-01-08T20:21:26Z")

</div>

If I set up a software timer, can it interrupt running application code arbitrarily? Only during `delay()`s?

How do the various `SYSTEM_MODE()`s affect this?

Basically, I’m trying to determine what level of synchronization I need between a timer which produces some data, and an application routine in `loop()` which consumes the same.

---

<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:** [January 8, 2018, 9:28pm UTC](https://community.particle.io/t/are-timers-preemptive/38574/2 "2018-01-08T21:28:59Z")

</div>

Software Timers are run on a dedicated thread, so one timer will _not_ “interrupt” another timer, but timers can “interrupt” `loop()`

The FreeRTOS threads get a 1ms time slice each and the switch can happen “any” time (unless sync constructs prevent a switch - see [https://docs.particle.io/reference/firmware/photon/#task-switching](https://docs.particle.io/reference/firmware/photon/#task-switching))

---

<div class="post-metadata">

**Author:** ![AustinGlaser](https://avatars.discourse-cdn.com/v4/letter/a/ed655f/32.png) [@AustinGlaser](https://community.particle.io/u/AustinGlaser)\
**Post date:** [January 8, 2018, 9:46pm UTC](https://community.particle.io/t/are-timers-preemptive/38574/3 "2018-01-08T21:46:28Z")

</div>

Great! So protection of concurrent accesses should be done with `SINGLE_THREADED_BLOCK()`, I take it?

---

<div class="post-metadata">

**Author:** ![peekay123](https://sea2.discourse-cdn.com/flex026/user_avatar/community.particle.io/peekay123/32/810_2.png) [@peekay123](https://community.particle.io/u/peekay123)\
**Post date:** [January 9, 2018, 12:20am UTC](https://community.particle.io/t/are-timers-preemptive/38574/4 "2018-01-09T00:20:36Z")

</div>

@AustinGlaser, yes, that is the way to go since mutexes are not exposed on the user API.
