Suppose I have a class that looks like this (actually this is the size):
class K { public: long long get_x() const;
As you can see, this should be thread safe. The update function is called by one thread at a time, but it is unknown, but only one thread (guaranteed), but the accessor can be called by several threads at the same time.
The update function changes all values and is called very often (hundread time per second). The current implementation, as you can guess, will block a lot.
I considered using std :: atomic to avoid blocking and potentially make this code more efficient. However, I really need an update function to update the values together. So I'm considering doing something like this:
class K { public: long long get_x() const { return data.load().x; } void update( long long w ) { auto data_now = data.load();
My current understanding of std :: atomic is that even if this code is more readable than the previous one (since it has no lock declarations everywhere) , since the K :: Data structure is “big”, std :: atomic will just be implemented with a normal mutex lock (so it should not be faster than my original implementation).
Am I right?
source share