Consider:
class Vector
{
double x, y, z;
// …
};
class Object
{
Vector Vec1, Vec2;
std::mutex Mtx1, Mtx2;
void ModifyVec1() { std::lock_guard Lock(Mtx1); /* … */ }
void ModifyVec2() { std::lock_guard Lock(Mtx2); /* … */ }
};
If either the mutexes or the guarded variables are stored contiguously and they share a cache line when cached, can this cause a sort of “cross-locking”?
If so, is it a good practice to declare the mutexes right after (or before) the variable they guard?
Aligning the class to std::hardware_destructive_interference_size
(P0154) might avoid this effect. Are the potential benefits worth the overalignment of the object?