For instance, here is my thread barrier/section type thing
template <bool switchable = false, bool counting = false>
struct SimpleBarrier {
private:
std::mutex mtx;
std::condition_variable cv;
std::atomic<bool> enabled;
std::atomic<int> inside;
public:
SimpleBarrier() {
if (switchable) enabled.store(true, std::memory_order_release);
if (counting) inside.store(0, std::memory_order_release);
}
void enter() {
if (switchable && !enabled.load(std::memory_order_acquire)) return;
if (counting) inside.fetch_add(1, std::memory_order_acq_rel);
std::unique_lock<std::mutex> lock(mtx);
cv.wait(lock);
}
void leave() {
if (counting) inside.fetch_sub(1, std::memory_order_acq_rel);
}
void release() {
cv.notify_all();
}
void enable() {
if (switchable) {
enabled.store(true, std::memory_order_release);
}
}
void disable() {
if (switchable) {
enabled.store(false, std::memory_order_release);
}
}
bool is_empty() {
if (counting) return inside.load(std::memory_order_acquire) == 0;
return false;
}
};
Initially I was just going to make a bunch of classes like "SwitchableBarrier" and "CountingSection" etc but then I started needing combinations like "SwitchableCountingSection" and there are probably going to be more qualities so to avoid making a dozen classes, I rolled them into one class using template parameters to turn certain qualities on/off.
Using template parameters as conditionals allows the compiler to optimize away unused qualities so there's no extra overhead from unused ones. The question is though - is it possible to also make it optimize away class variables?
Take the variable "inside" for instance, if counting == false then it is completely useless and doesn't need to be there. Can I have template parameter specific class variables?
Aucun commentaire:
Enregistrer un commentaire