提交 e7a15aca 编写于 作者: A azural 提交者: Zhongjun Ni

framework: Make CacheBuffer copy-constructor thread-safe

上级 d551ea6b
......@@ -35,25 +35,26 @@ class CacheBuffer {
buffer_.resize(capacity_);
}
CacheBuffer(CacheBuffer&& rhs) {
CacheBuffer(const CacheBuffer& rhs) {
std::lock_guard<std::mutex> lg(rhs.mutex_);
head_ = rhs.head_;
tail_ = rhs.head_;
buffer_ = rhs.buffer_;
capacity_ = rhs.capacity_;
}
T& operator[](uint64_t pos) { return buffer_[GetIndex(pos)]; }
const T& at(uint64_t pos) { return buffer_[GetIndex(pos)]; }
T& operator[](const uint64_t& pos) { return buffer_[GetIndex(pos)]; }
const T& at(const uint64_t& pos) const { return buffer_[GetIndex(pos)]; }
uint64_t Head() { return head_ + 1; }
uint64_t Tail() { return tail_; }
uint64_t Size() { return tail_ - head_; }
uint64_t Head() const { return head_ + 1; }
uint64_t Tail() const { return tail_; }
uint64_t Size() const { return tail_ - head_; }
const T& Front() { return buffer_[GetIndex(head_ + 1)]; }
const T& Back() { return buffer_[GetIndex(tail_)]; }
const T& Front() const { return buffer_[GetIndex(head_ + 1)]; }
const T& Back() const { return buffer_[GetIndex(tail_)]; }
bool Empty() { return tail_ == 0; }
bool Full() { return capacity_ - 1 == tail_ - head_; }
bool Empty() const { return tail_ == 0; }
bool Full() const { return capacity_ - 1 == tail_ - head_; }
void Fill(const T& value) {
if (Full()) {
......@@ -69,14 +70,14 @@ class CacheBuffer {
std::mutex& Mutex() { return mutex_; }
private:
CacheBuffer(const CacheBuffer& other) = delete;
CacheBuffer& operator=(const CacheBuffer& other) = delete;
uint64_t GetIndex(uint64_t pos) { return pos % capacity_; }
std::vector<T> buffer_;
uint64_t GetIndex(const uint64_t& pos) const { return pos % capacity_; }
uint64_t head_ = 0;
uint64_t tail_ = 0;
uint64_t capacity_ = 0;
std::mutex mutex_;
std::vector<T> buffer_;
mutable std::mutex mutex_;
};
} // namespace data
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册