Skip to content

Commit d6573ef

Browse files
committed
Improve: Use hardware_destructive_interference_size
1 parent ccd496d commit d6573ef

File tree

1 file changed

+12
-1
lines changed

1 file changed

+12
-1
lines changed

include/fork_union.hpp

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
#include <atomic> // `std::atomic`
1111
#include <cstddef> // `std::max_align_t`
1212
#include <cassert> // `assert`
13+
#include <new> // `std::hardware_destructive_interference_size`
1314

1415
#define FORK_UNION_VERSION_MAJOR 0
1516
#define FORK_UNION_VERSION_MINOR 3
@@ -39,6 +40,16 @@
3940

4041
namespace ashvardanian {
4142

43+
/**
44+
* @brief Defines variable alignment to avoid false sharing.
45+
* @see https://en.cppreference.com/w/cpp/thread/hardware_destructive_interference_size
46+
*/
47+
#if defined(__cpp_lib_hardware_interference_size)
48+
static constexpr std::size_t default_alignment_k = std::hardware_destructive_interference_size;
49+
#else
50+
static constexpr std::size_t default_alignment_k = alignof(std::max_align_t);
51+
#endif
52+
4253
/**
4354
* @brief Minimalistic STL-based non-resizable thread-pool for simultaneous blocking tasks.
4455
*
@@ -57,7 +68,7 @@ namespace ashvardanian {
5768
* use the "acquire-release" model, and some going further to "relaxed" model.
5869
* @see https://en.cppreference.com/w/cpp/atomic/memory_order#Release-Acquire_ordering
5970
*/
60-
template <typename allocator_type_ = std::allocator<std::byte>, std::size_t alignment_ = alignof(std::max_align_t)>
71+
template <typename allocator_type_ = std::allocator<std::byte>, std::size_t alignment_ = default_alignment_k>
6172
class fork_union {
6273
public:
6374
using allocator_t = allocator_type_;

0 commit comments

Comments
 (0)