Skip to content

Commit 2b8d44a

Browse files
feat: PR the relative issue with #50 (#51)
1 parent af7e361 commit 2b8d44a

33 files changed

Lines changed: 1095 additions & 1087 deletions

code/examples/chapter07/03_circular_buffer/CMakeLists.txt renamed to code/examples/chapter08/08_circular_buffer/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
cmake_minimum_required(VERSION 3.20)
2-
project(chapter07_03_circular_buffer CXX)
2+
project(chapter08_08_circular_buffer CXX)
33

44
set(CMAKE_CXX_STANDARD 17)
55
set(CMAKE_CXX_STANDARD_REQUIRED ON)
File renamed without changes.

code/examples/chapter07/03_circular_buffer/atomic_ring_buffer.h renamed to code/examples/chapter08/08_circular_buffer/atomic_ring_buffer.h

Lines changed: 68 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -1,69 +1,68 @@
1-
// atomic_ring_buffer.h - 线程安全的循环缓冲区实现
2-
#pragma once
3-
#include <cstddef>
4-
#include <array>
5-
#include <atomic>
6-
7-
template<typename T, std::size_t Capacity>
8-
class AtomicRingBuffer {
9-
public:
10-
AtomicRingBuffer() : head_(0), tail_(0) {}
11-
12-
bool push(const T& value) {
13-
std::size_t head = head_.load(std::memory_order_relaxed);
14-
std::size_t next_head = (head + 1) % Capacity;
15-
std::size_t tail = tail_.load(std::memory_order_acquire);
16-
17-
if (next_head == tail) {
18-
return false; // 缓冲区满了
19-
}
20-
21-
buffer_[head] = value;
22-
head_.store(next_head, std::memory_order_release);
23-
return true;
24-
}
25-
26-
bool pop(T& out) {
27-
std::size_t tail = tail_.load(std::memory_order_relaxed);
28-
std::size_t head = head_.load(std::memory_order_acquire);
29-
30-
if (tail == head) {
31-
return false; // 没数据
32-
}
33-
34-
out = buffer_[tail];
35-
std::size_t next_tail = (tail + 1) % Capacity;
36-
tail_.store(next_tail, std::memory_order_release);
37-
return true;
38-
}
39-
40-
bool empty() const {
41-
std::size_t head = head_.load(std::memory_order_acquire);
42-
std::size_t tail = tail_.load(std::memory_order_acquire);
43-
return head == tail;
44-
}
45-
46-
bool full() const {
47-
std::size_t head = head_.load(std::memory_order_acquire);
48-
std::size_t tail = tail_.load(std::memory_order_acquire);
49-
return (head + 1) % Capacity == tail;
50-
}
51-
52-
std::size_t size() const {
53-
std::size_t head = head_.load(std::memory_order_acquire);
54-
std::size_t tail = tail_.load(std::memory_order_acquire);
55-
if (head >= tail) {
56-
return head - tail;
57-
}
58-
return Capacity - (tail - head);
59-
}
60-
61-
std::size_t capacity() const {
62-
return Capacity - 1; // 实际可用容量
63-
}
64-
65-
private:
66-
std::array<T, Capacity> buffer_;
67-
std::atomic<std::size_t> head_;
68-
std::atomic<std::size_t> tail_;
69-
};
1+
// atomic_ring_buffer.h - 线程安全的循环缓冲区实现
2+
#pragma once
3+
#include <array>
4+
#include <atomic>
5+
#include <cstddef>
6+
7+
template <typename T, std::size_t Capacity> class AtomicRingBuffer {
8+
public:
9+
AtomicRingBuffer() : head_(0), tail_(0) {}
10+
11+
bool push(const T& value) {
12+
std::size_t head = head_.load(std::memory_order_relaxed);
13+
std::size_t next_head = (head + 1) % Capacity;
14+
std::size_t tail = tail_.load(std::memory_order_acquire);
15+
16+
if (next_head == tail) {
17+
return false; // 缓冲区满了
18+
}
19+
20+
buffer_[head] = value;
21+
head_.store(next_head, std::memory_order_release);
22+
return true;
23+
}
24+
25+
bool pop(T& out) {
26+
std::size_t tail = tail_.load(std::memory_order_relaxed);
27+
std::size_t head = head_.load(std::memory_order_acquire);
28+
29+
if (tail == head) {
30+
return false; // 没数据
31+
}
32+
33+
out = buffer_[tail];
34+
std::size_t next_tail = (tail + 1) % Capacity;
35+
tail_.store(next_tail, std::memory_order_release);
36+
return true;
37+
}
38+
39+
bool empty() const {
40+
std::size_t head = head_.load(std::memory_order_acquire);
41+
std::size_t tail = tail_.load(std::memory_order_acquire);
42+
return head == tail;
43+
}
44+
45+
bool full() const {
46+
std::size_t head = head_.load(std::memory_order_acquire);
47+
std::size_t tail = tail_.load(std::memory_order_acquire);
48+
return (head + 1) % Capacity == tail;
49+
}
50+
51+
std::size_t size() const {
52+
std::size_t head = head_.load(std::memory_order_acquire);
53+
std::size_t tail = tail_.load(std::memory_order_acquire);
54+
if (head >= tail) {
55+
return head - tail;
56+
}
57+
return Capacity - (tail - head);
58+
}
59+
60+
std::size_t capacity() const {
61+
return Capacity - 1; // 实际可用容量
62+
}
63+
64+
private:
65+
std::array<T, Capacity> buffer_;
66+
std::atomic<std::size_t> head_;
67+
std::atomic<std::size_t> tail_;
68+
};

code/examples/chapter07/03_circular_buffer/basic_example.cpp renamed to code/examples/chapter08/08_circular_buffer/basic_example.cpp

Lines changed: 50 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -1,50 +1,50 @@
1-
// basic_example.cpp - 循环缓冲区基本用法演示
2-
#include "ring_buffer.h"
3-
#include <iostream>
4-
5-
int main() {
6-
// 创建一个容量为 16 的循环缓冲区(实际可用 15)
7-
RingBuffer<int, 16> buffer;
8-
9-
std::cout << "Ring Buffer Demo\n";
10-
std::cout << "Capacity: " << buffer.capacity() << "\n\n";
11-
12-
// 测试:填充和读取
13-
std::cout << "=== Pushing 10 elements ===\n";
14-
for (int i = 0; i < 10; ++i) {
15-
if (buffer.push(i)) {
16-
std::cout << "Pushed: " << i << ", size: " << buffer.size() << '\n';
17-
}
18-
}
19-
20-
std::cout << "\n=== Popping 5 elements ===\n";
21-
for (int i = 0; i < 5; ++i) {
22-
int value;
23-
if (buffer.pop(value)) {
24-
std::cout << "Popped: " << value << ", size: " << buffer.size() << '\n';
25-
}
26-
}
27-
28-
std::cout << "\n=== Pushing 8 more elements ===\n";
29-
for (int i = 10; i < 18; ++i) {
30-
if (buffer.push(i)) {
31-
std::cout << "Pushed: " << i << ", size: " << buffer.size() << '\n';
32-
} else {
33-
std::cout << "Failed to push: " << i << " (buffer full)\n";
34-
}
35-
}
36-
37-
std::cout << "\n=== Remaining contents ===\n";
38-
while (!buffer.empty()) {
39-
int value;
40-
buffer.pop(value);
41-
std::cout << value << ' ';
42-
}
43-
std::cout << "\n";
44-
45-
std::cout << "\nFinal size: " << buffer.size() << '\n';
46-
std::cout << "Empty: " << (buffer.empty() ? "yes" : "no") << '\n';
47-
std::cout << "Full: " << (buffer.full() ? "yes" : "no") << '\n';
48-
49-
return 0;
50-
}
1+
// basic_example.cpp - 循环缓冲区基本用法演示
2+
#include "ring_buffer.h"
3+
#include <iostream>
4+
5+
int main() {
6+
// 创建一个容量为 16 的循环缓冲区(实际可用 15)
7+
RingBuffer<int, 16> buffer;
8+
9+
std::cout << "Ring Buffer Demo\n";
10+
std::cout << "Capacity: " << buffer.capacity() << "\n\n";
11+
12+
// 测试:填充和读取
13+
std::cout << "=== Pushing 10 elements ===\n";
14+
for (int i = 0; i < 10; ++i) {
15+
if (buffer.push(i)) {
16+
std::cout << "Pushed: " << i << ", size: " << buffer.size() << '\n';
17+
}
18+
}
19+
20+
std::cout << "\n=== Popping 5 elements ===\n";
21+
for (int i = 0; i < 5; ++i) {
22+
int value;
23+
if (buffer.pop(value)) {
24+
std::cout << "Popped: " << value << ", size: " << buffer.size() << '\n';
25+
}
26+
}
27+
28+
std::cout << "\n=== Pushing 8 more elements ===\n";
29+
for (int i = 10; i < 18; ++i) {
30+
if (buffer.push(i)) {
31+
std::cout << "Pushed: " << i << ", size: " << buffer.size() << '\n';
32+
} else {
33+
std::cout << "Failed to push: " << i << " (buffer full)\n";
34+
}
35+
}
36+
37+
std::cout << "\n=== Remaining contents ===\n";
38+
while (!buffer.empty()) {
39+
int value;
40+
buffer.pop(value);
41+
std::cout << value << ' ';
42+
}
43+
std::cout << "\n";
44+
45+
std::cout << "\nFinal size: " << buffer.size() << '\n';
46+
std::cout << "Empty: " << (buffer.empty() ? "yes" : "no") << '\n';
47+
std::cout << "Full: " << (buffer.full() ? "yes" : "no") << '\n';
48+
49+
return 0;
50+
}

code/examples/chapter07/03_circular_buffer/ring_buffer.h renamed to code/examples/chapter08/08_circular_buffer/ring_buffer.h

Lines changed: 49 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -1,56 +1,49 @@
1-
// ring_buffer.h - 简单的循环缓冲区实现
2-
#pragma once
3-
#include <cstddef>
4-
#include <array>
5-
6-
template<typename T, std::size_t Capacity>
7-
class RingBuffer {
8-
public:
9-
bool push(const T& value) {
10-
if (full()) {
11-
return false; // 缓冲区满了
12-
}
13-
14-
buffer_[head_] = value;
15-
head_ = (head_ + 1) % Capacity;
16-
return true;
17-
}
18-
19-
bool pop(T& out) {
20-
if (empty()) {
21-
return false; // 没数据
22-
}
23-
24-
out = buffer_[tail_];
25-
tail_ = (tail_ + 1) % Capacity;
26-
return true;
27-
}
28-
29-
bool empty() const {
30-
return head_ == tail_;
31-
}
32-
33-
bool full() const {
34-
return (head_ + 1) % Capacity == tail_;
35-
}
36-
37-
std::size_t size() const {
38-
if (head_ >= tail_) {
39-
return head_ - tail_;
40-
}
41-
return Capacity - (tail_ - head_);
42-
}
43-
44-
std::size_t capacity() const {
45-
return Capacity - 1; // 实际可用容量
46-
}
47-
48-
void clear() {
49-
head_ = tail_ = 0;
50-
}
51-
52-
private:
53-
std::array<T, Capacity> buffer_{};
54-
std::size_t head_ = 0;
55-
std::size_t tail_ = 0;
56-
};
1+
// ring_buffer.h - 简单的循环缓冲区实现
2+
#pragma once
3+
#include <array>
4+
#include <cstddef>
5+
6+
template <typename T, std::size_t Capacity> class RingBuffer {
7+
public:
8+
bool push(const T& value) {
9+
if (full()) {
10+
return false; // 缓冲区满了
11+
}
12+
13+
buffer_[head_] = value;
14+
head_ = (head_ + 1) % Capacity;
15+
return true;
16+
}
17+
18+
bool pop(T& out) {
19+
if (empty()) {
20+
return false; // 没数据
21+
}
22+
23+
out = buffer_[tail_];
24+
tail_ = (tail_ + 1) % Capacity;
25+
return true;
26+
}
27+
28+
bool empty() const { return head_ == tail_; }
29+
30+
bool full() const { return (head_ + 1) % Capacity == tail_; }
31+
32+
std::size_t size() const {
33+
if (head_ >= tail_) {
34+
return head_ - tail_;
35+
}
36+
return Capacity - (tail_ - head_);
37+
}
38+
39+
std::size_t capacity() const {
40+
return Capacity - 1; // 实际可用容量
41+
}
42+
43+
void clear() { head_ = tail_ = 0; }
44+
45+
private:
46+
std::array<T, Capacity> buffer_{};
47+
std::size_t head_ = 0;
48+
std::size_t tail_ = 0;
49+
};

0 commit comments

Comments
 (0)