Skip to content

Instantly share code, notes, and snippets.

@Joshhua5 Joshhua5/Queue.h Secret
Created Jun 6, 2015

Embed
What would you like to do?
Queue False Sharing
//
// Queue.cpp
//
// Created by Joshua Waring on 1/06/2015.
//
//
#ifdef FALSE_SHARING_ALLOWED
template<typedef T>
class alignas(64) Queue{
T* data_;
size_t push_position_;
size_t pop_position_;
std::atomic<size_t> size_;
size_t capacity_;
};
// Accounting for False Sharing
// To do this we'll break the memory into 3 cache lines,
// One for each of the two cores and one for data that must be shared
#else
template<typedef T>
class alignas(64) Queue{
// Producers C-Line
size_t push_position_;
char pad_p[64 - sizeof(size_t)];
// Consumors C-Line
size_t pop_position_;
char pad_c[64 - sizeof(size_t)];
// Shared Read C-Line
T* data_;
size_t capacity_;
char pad_sr[64 - sizeof(size_t) - sizeof(T*)];
// Shared Write C-Line
std::atomic<size_t> size_;
char pad_sw[64 - sizeof(std::atomic<size_t>)];
};
#endif
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.