Miscellaneous
lockfree
is a collection of lock-free data structures written in standard C++11 and suitable for all platforms - from deeply embedded to HPC.
Lock-free data structures are data structures that are thread and interrupt safe for concurrent use without having to use mutual exclusion mechanisms. They are most useful for inter process communication, and often scale much better than lock-based structures with the number of operations and threads.
lockfree
These data structures are more performant and should generally be used whenever there is only one thread/interrupt pushing data and another one retrieving it.
These data structures are more general, supporting multiple producers and consumers at the same time, however they have storage and performance overhead compared to single producer single consumer data structures. They also require atomic instructions which can be missing from some low-end microcontrollers.
There are three main ways to get the library:
lockfree
uses cacheline alignment for indexes to avoid the False Sharing phenomenon by default, avoiding the performance loss of cacheline invalidation on cache coherent systems. This aligns the indexes to LOCKFREE_CACHELINE_LENGTH
, 64
by default.
On embedded systems, LOCKFREE_CACHE_COHERENT
should almost always be set as false
to avoid wasting memory.
Additionally, some systems have a non-typical cacheline length (for instance the apple M1/M2 CPUs have a cacheline length of 128 bytes), and LOCKFREE_CACHELINE_LENGTH
should be set accordingly in those cases.
All of the data structures in lockfree
are only meant to be used for trivial types.
The biggest reason you would want to use a lock-free data structure on hosted environments would be avoiding issues surrounding locking such as deadlocks, priority inversion and nondeterministic access latency. When used properly, lock-free data structures can also improve performance in some scenarios.
Additionally, lockfree
provides a way to build applications and libraries that can be compiled to work on both POSIX and non-POSIX environments without #ifdef
s or polymorphism.
While locking usually isn't expensive on embedded systems such as microcontrollers, there is a wide variety of RTOS-es and no standardized API for locking. The fact that multiple architectures are present from 8051 to RISC-V means that interrupt management methods are not standardized either.
lockfree
provides a way to build portable embedded code with a negligible performance cost as opposed to locking, code using lockfree
can be compiled to run on any embedded platform supporting C++11. Additionally, the code can easily be tested on a host machine without the need for mocking.
lockfree
?All structures in lockfree
are bounded, array-based and lock-free, spsc data structures are also waitfree and termination safe.
For more insight into lock-free programming, take a look at: