Re: Atomic() vs k_sched_lock() vs irq_lock() vs k_mutex_lock()

Boie, Andrew P

You should be using the APIs in include/atomic.h


On arches that don't natively support atomic operations, these get implemented in terms of irq_lock()




From: devel@... [mailto:devel@...] On Behalf Of Kim Bøndergaard
Sent: Monday, April 1, 2019 6:53 AM
To: Zephyr-devel@...
Subject: [Zephyr-devel] Atomic() vs k_sched_lock() vs irq_lock() vs k_mutex_lock()


We have an internal discussion about the recommended usage of the 4 methods mentioned in subjects, when the goal is to protect an assignment of a variable (u32 - any platform) within one task so it is not resulting in an invalid reading from a higher priority task.


Mutex'es are usually good since they only disturb the threads being involved in accessing the shared variable - but are they overkill in this case?

irq_lock() sounds like something fast but effects the entire system

k_sched_lock() is fast (according to the manual) but effects all threads as I see it.


atomic is probably a good option, but what's the efficiency on e.g. an ARM platform not natively supporting atomic's.


Any ideas or recommendation about when to use what solution?



Kim Bøndergaard
Prevas A/S
Team Manager / Systems Architect
Hedeager 3, DK-8200 Aarhus N

Phone +45 3315 9090
Mobile +45 5154 3961



Join to automatically receive all group messages.