martinandrovich / rb-pro5

The project combines the fields of robotic path planning, computer vision and artificial intelligence, to be implemented as algorithms with the purpose of controlling a robot within a simulated environment.
3 stars 0 forks source link

Make all custom types atomic with get() and get() methods #3

Open martinandrovich opened 4 years ago

martinandrovich commented 4 years ago

All custom types should be atomic by default, such that it's not required to manually used a mutex to have thread-safe access to the object data. Data should be updated using the .set() method, whereas accessing data atomically can be implemented in different ways.

For example, using the lidar_t lidar_data; object, accessing a the stored parameter range_max:

// unsafe
// .range_max is public
auto value = lidar_data.range_max;

// semi-safe
// tempated .get() method; .range_max is still public
auto value = lidar_data.get(lidar_data.range_max);

// safe
// manual get methods; .range_max is now private
auto value = lidar_data.get_range_max();

This can also be guaranteed by implementing std::atomic on the public members, such that individual access is guaranteed.

martinandrovich commented 4 years ago

Should probably be done in coherence with #2 .