Note that there are some explanatory texts on larger screens.

plurals
  1. POC++/Boost: Synchronize access to a resource across multiple method (getter) calls
    primarykey
    data
    text
    <p>I'm not sure if this is a question regarding programming technique or design but I'm open for suggestions.</p> <p>The problem: I want to create an abstraction layer between data sources (sensors) and consumers. The idea is that the consumers only "know" the interfaces (abstract base class) of different sensor types. Each of this sensor types usually consists of several individual values which all have their own getter methods. </p> <p>As an example I will use a simplified GPS sensor.</p> <pre><code>class IGpsSensor { public: virtual float getLongitude() = 0; virtual float getLatitude() = 0; virtual float getElevation() = 0; // Deviations virtual float getLongitudeDev() = 0; virtual float getLatitudeDev() = 0; virtual float getElevationDev() = 0; virtual int getNumOfSatellites() = 0; }; </code></pre> <p>Since updates to the sensor are done by a different thread (details are up to the implementation of the interface), synchronizing getters and also the update methods seems like a reasonable approach to ensure consistency.</p> <p>So far so good. In most cases this level of synchronization should suffice. However, sometimes it might be necessary to aquire more than one value (with consecutive getXXX() calls) and ensure that no update is happening in between. Whether this is necessary or not (and which values are important) is up to the consumer. </p> <p>Sticking to the example, in a lot of cases it is only important to know longitude and latitude (but hopefully both relating to the same update()). I admit that this could be done be grouping them together into a "Position" class or struct. But a consumer might also use the sensor for a more complicated algorithm and requires the deviation as well. </p> <p>Now I was wondering, what would be a proper way to do this. </p> <p>Solutions I could think of:</p> <ul> <li><p>Group all possible values into a struct (or class) and add an additional (synchronized) getter returning copies of all values at once - seems like a lot of unnecessary overhead to me in case only 2 or 3 out of maybe 10 values are needed.</p></li> <li><p>Add a method returning a reference to the mutex used within the data source to allow locking by the consumer - this doesn't feel like "good design". And since getters are already synchronized, using a recursive mutex is mandatory. However, I assume that there are multiple readers but only one writer and thus I'd rather go with a shared mutex here.</p></li> </ul> <p>Thanks for your help.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload