Add memory padding and alignment to prevent false sharing
[folly.git] / folly / ProducerConsumerQueue.h
index 053c0e264b649075b2d148af5dc199b83ad95b22..9ebe65acb46c66dbef1f9ab33e77e07fd968bdeb 100644 (file)
@@ -27,6 +27,8 @@
 #include <type_traits>
 #include <utility>
 
+#include <folly/detail/CacheLocality.h>
+
 namespace folly {
 
 /*
@@ -166,11 +168,14 @@ struct ProducerConsumerQueue {
   }
 
 private:
+  char pad0_[detail::CacheLocality::kFalseSharingRange];
   const uint32_t size_;
   T* const records_;
 
-  std::atomic<unsigned int> readIndex_;
-  std::atomic<unsigned int> writeIndex_;
+  FOLLY_ALIGN_TO_AVOID_FALSE_SHARING std::atomic<unsigned int> readIndex_;
+  FOLLY_ALIGN_TO_AVOID_FALSE_SHARING std::atomic<unsigned int> writeIndex_;
+
+  char pad1_[detail::CacheLocality::kFalseSharingRange - sizeof(writeIndex_)];
 };
 
 }