-
-
Save Joshhua5/2ce996a08effe44f2f0f to your computer and use it in GitHub Desktop.
Queue False Sharing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// 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