What are the difference between the usage of critical sections and nrf_atomic operations?
Using critical sections disable all interrupts temporarily and re-enables them again. This is no problem as long at it is for short times, but it has a performance penalty and you block potentially unrelated high priority interrupts.
The atomic operations module use the LDREX/STREX instructions that are present in the Cortex-M4 core. This is used to make a form of mutex to make sure that buffer is not updated by one “user” while it is being manipulated by another user without the need for critical sections.
Frequency Asked Question (FAQ)
How does it work for example if I want to get 5 elements and all of them are of different sizes? As far as I can tell the library doesn’t support stuff like this.
You need to use your own scheme on top if you need it to handle different objects, for instance a TLV (type-length-value) variant.
When is thread safety important? When protecting against interrupts? Or is this an RTOS thing?
It is important if you are using the library from several interrupt priorities, regardless of it is using an RTOS or not. If it is possible that a fifo operation can be preempted by another fifo operation, then that is a concern.