// // Created by loki on 16-4-19. // #ifndef SUNSHINE_SYNC_H #define SUNSHINE_SYNC_H #include #include #include namespace util { template class sync_t { public: static_assert(N > 0, "sync_t should have more than zero mutexes"); using value_type = T; template std::lock_guard lock() { return std::lock_guard { std::get(_lock) }; } template sync_t(Args&&... args) : raw {std::forward(args)... } {} sync_t &operator=(sync_t &&other) noexcept { for(auto &l : _lock) { l.lock(); } for(auto &l : other._lock) { l.lock(); } raw = std::move(other.raw); for(auto &l : _lock) { l.unlock(); } for(auto &l : other._lock) { l.unlock(); } return *this; } sync_t &operator=(sync_t &other) noexcept { for(auto &l : _lock) { l.lock(); } for(auto &l : other._lock) { l.lock(); } raw = other.raw; for(auto &l : _lock) { l.unlock(); } for(auto &l : other._lock) { l.unlock(); } return *this; } sync_t &operator=(const value_type &val) noexcept { for(auto &l : _lock) { l.lock(); } raw = val; for(auto &l : _lock) { l.unlock(); } return *this; } sync_t &operator=(value_type &&val) noexcept { for(auto &l : _lock) { l.lock(); } raw = std::move(val); for(auto &l : _lock) { l.unlock(); } return *this; } value_type *operator->() { return &raw; } value_type raw; private: std::array _lock; }; } #endif //T_MAN_SYNC_H