Skip to content

Instantly share code, notes, and snippets.

@Joshhua5

Joshhua5/Queue.h Secret

Created June 6, 2015 10:35
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Joshhua5/2ce996a08effe44f2f0f to your computer and use it in GitHub Desktop.
Save Joshhua5/2ce996a08effe44f2f0f to your computer and use it in GitHub Desktop.
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