template<typename struct_type>
class protected_struct< struct_type >
A RAII type used to make structs thread-safe for FreeRTOS via mutexes.
- Template Parameters
-
| struct_type | The struct to make thread-safe |
This class gives you the ability to make a struct thread-safe and then access it with an automatically managed FreeRTOS mutex, which automatically locks on construction and unlocks on destruction (RAII-Principle).
Ways to access members of the protected struct:
1) Single Access: Using the dereferenced pointer operator directly on the protected_struct instance
protected_struct_instance->value = 5;
This creates a temporary lock for the duration of the single expression. This is convenient for quick, atomic operations but can be inefficient for multiple accesses within a single logical block.
2) Full Scope Access: Using the lock() method to obtain a mutex lock object
auto locked_data = protected_struct_instance.lock();
locked_data->value = 5;
This acquires the mutex and locks the struct for the entire lifetime of the locked_data object. The mutex is automatically released when locked_data goes out of scope. This is efficient for performing multiple operations on the protected struct within a single logical block. You can also release the mutex early by calling locked_data.unlock()