This is a technique I learnt about from Jason Gregory's excellent book, Game Engine Architecture (3rd Edition) . If you have a shared resource accessed by multiple threads, where you're fairly certain that it's only ever accessed by one thread at a time, you can use an assert() to check for this at debug time without having to pay the runtime cost of locking a mutex. The implementation is fairly straightforward: class UnnecessaryMutex { public: void lock() { assert(!_locked); _locked = true; } void unlock() { assert(_locked); _locked = false; } private: volatile bool _locked = false; }; #ifdef ENABLE_LOCK_ASSERTS #define BEGIN_ASSERT_LOCK_NOT_REQUIRED(mutex) (mutex).lock() #define END_ASSERT_LOCK_NOT_REQUIRED(mutex) (mutex).unlock() #else #define BEGIN_ASSERT_LOCK_NOT_REQUIRED(mutex) #define END_ASSERT_LOCK_NOT_REQUIRED(mutex) #endif Usage is equally straightforward: UnnecessaryMutex gMutex; void PossiblyOverlappingFunction
Just thought of a way to store the bounding box for a single triangle in only one byte. It's not really practical or something you'd ever really want to use, but what the hell. Assume we have some kind of indexed mesh structure with a list of vertex positions and a list of triangle indices: struct Mesh { std::vector<vec3> verts; std::vector<uvec3> triangles; }; We can find the bounding box of a triangle by taking the min and max of all three vertices: vec3 Mesh::lowerBound(uint32_t tri) const { vec3 v0 = verts[triangles[tri].x]; vec3 v1 = verts[triangles[tri].y]; vec3 v2 = verts[triangles[tri].z]; return min(min(v0, v1), v2); } vec3 Mesh::upperBound(uint32_t tri) const { vec3 v0 = verts[triangles[tri].x]; vec3 v1 = verts[triangles[tri].y]; vec3 v2 = verts[triangles[tri].z]; return max(max(v0, v1), v2); } This is nice and simple and probably way better than what I'm about to suggest. W