Boboter
Loading...
Searching...
No Matches
protected_struct< struct_type > Class Template Reference

A RAII type used to make structs thread-safe for FreeRTOS via mutexes. More...

#include <protected_struct.h>

Public Member Functions

 protected_struct ()
 ~protected_struct ()
 protected_struct (const protected_struct &)=delete
protected_structoperator= (const protected_struct &)=delete
mutex_lock operator-> () const
 Overload of the get value from dereferenced pointer operator to make it possible to get a value using it.
mutex_lock lock () const
 Locks the struct and holds the mutex for the returned object's scope.

Detailed Description

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_typeThe 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;
// ... more operations on locked_data-> ...

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()

Constructor & Destructor Documentation

◆ protected_struct() [1/2]

template<typename struct_type>
protected_struct< struct_type >::protected_struct ( )
inlineexplicit

◆ ~protected_struct()

template<typename struct_type>
protected_struct< struct_type >::~protected_struct ( )
inline

◆ protected_struct() [2/2]

template<typename struct_type>
protected_struct< struct_type >::protected_struct ( const protected_struct< struct_type > & )
delete

Member Function Documentation

◆ lock()

template<typename struct_type>
mutex_lock protected_struct< struct_type >::lock ( ) const
inline

Locks the struct and holds the mutex for the returned object's scope.

Returns
A mutex_lock object to hold the mutex and provide access to the protected data

◆ operator->()

template<typename struct_type>
mutex_lock protected_struct< struct_type >::operator-> ( ) const
inline

Overload of the get value from dereferenced pointer operator to make it possible to get a value using it.

Returns
A temporary mutex_lock object to hold the mutex and return the value

◆ operator=()

template<typename struct_type>
protected_struct & protected_struct< struct_type >::operator= ( const protected_struct< struct_type > & )
delete

The documentation for this class was generated from the following file: