Skip to content

Commit 72094c5

Browse files
authored
Ruilizhenhu/fix(edition) 20250904 (#14)
* fix(edition): change edition no in cmakelist and readme * feat(mpsc): add mpsc thread test * fix(mpsc): add algorithm header in mpsc test * feat(action): add build & test actions on macOS
1 parent 6e3a92a commit 72094c5

File tree

4 files changed

+77
-6
lines changed

4 files changed

+77
-6
lines changed

.github/workflows/cmake-multi-platform.yml

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,26 +23,42 @@ jobs:
2323
#
2424
# To add more build types (Release, Debug, RelWithDebInfo, etc.) customize the build_type list.
2525
matrix:
26-
os: [ubuntu-latest, windows-latest]
26+
os: [ubuntu-latest, windows-latest, macos-latest]
2727
build_type: [Release]
2828
c_compiler: [gcc, clang, cl]
29+
2930
include:
31+
# Windows
3032
- os: windows-latest
3133
c_compiler: cl
3234
cpp_compiler: cl
35+
- os: windows-latest
36+
c_compiler: gcc
37+
cpp_compiler: g++
38+
- os: windows-latest
39+
c_compiler: clang
40+
cpp_compiler: clang++
41+
42+
# Linux
3343
- os: ubuntu-latest
3444
c_compiler: gcc
3545
cpp_compiler: g++
3646
- os: ubuntu-latest
3747
c_compiler: clang
3848
cpp_compiler: clang++
39-
- os: windows-latest
40-
c_compiler: gcc
41-
- os: windows-latest
49+
50+
# macOS
51+
- os: macos-latest
4252
c_compiler: clang
53+
cpp_compiler: clang++
54+
4355
exclude:
4456
- os: ubuntu-latest
4557
c_compiler: cl
58+
- os: macos-latest
59+
c_compiler: cl
60+
- os: macos-latest
61+
c_compiler: gcc
4662

4763
steps:
4864
- uses: actions/checkout@v4

CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ cmake_minimum_required(VERSION 3.15)
22

33
project(
44
ring_buffer
5-
VERSION 1.0.3
5+
VERSION 0.1.0
66
LANGUAGES CXX)
77

88
set(CMAKE_CXX_STANDARD 20)

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ else()
9191
FetchContent_Declare(
9292
ring_buffer
9393
GIT_REPOSITORY https://github.com/HuRuilizhen/ring_buffer.git
94-
GIT_TAG v1.0.3) # latest stable version
94+
GIT_TAG v0.1.0) # latest stable version
9595
FetchContent_MakeAvailable(ring_buffer)
9696
endif()
9797

tests/mpsc_ring_buffer_test.cc

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@
22

33
#include <gtest/gtest.h>
44

5+
#include <algorithm>
6+
#include <thread>
7+
#include <vector>
8+
59
namespace {
610

711
struct Counter {
@@ -78,6 +82,57 @@ TEST(MPSCRingBufferTest, WrapAround) {
7882
EXPECT_FALSE(buffer.tryPop(value));
7983
}
8084

85+
TEST(MPSCRingBufferTest, MPSC) {
86+
constexpr int PRODUCERS = 4;
87+
constexpr int ITEMS_PER_PRODUCER = 500;
88+
constexpr int TOTAL_ITEMS = PRODUCERS * ITEMS_PER_PRODUCER;
89+
constexpr int CAPACITY = 128;
90+
91+
RingBuffer::MPSCRingBuffer<int> buffer(CAPACITY);
92+
std::atomic<int> produced{0};
93+
std::atomic<int> consumed{0};
94+
95+
std::vector<std::thread> producer_threads;
96+
for (int p = 0; p < PRODUCERS; ++p) {
97+
producer_threads.emplace_back([p, &buffer, &produced]() {
98+
for (int i = 0; i < ITEMS_PER_PRODUCER; ++i) {
99+
int value = p * ITEMS_PER_PRODUCER + i;
100+
while (true) {
101+
if (buffer.tryPush(value)) {
102+
produced.fetch_add(1, std::memory_order_relaxed);
103+
break;
104+
} else {
105+
std::this_thread::yield();
106+
}
107+
}
108+
}
109+
});
110+
}
111+
112+
std::vector<int> results;
113+
results.reserve(TOTAL_ITEMS);
114+
std::thread consumer_thread([&]() {
115+
while (consumed.load(std::memory_order_relaxed) < TOTAL_ITEMS) {
116+
int value;
117+
if (buffer.tryPop(value)) {
118+
results.push_back(value);
119+
consumed.fetch_add(1, std::memory_order_relaxed);
120+
} else {
121+
std::this_thread::yield();
122+
}
123+
}
124+
});
125+
126+
for (auto& t : producer_threads) t.join();
127+
consumer_thread.join();
128+
129+
EXPECT_EQ(results.size(), static_cast<size_t>(TOTAL_ITEMS));
130+
std::sort(results.begin(), results.end());
131+
for (int i = 0; i < TOTAL_ITEMS; ++i) {
132+
EXPECT_EQ(results[i], i);
133+
}
134+
}
135+
81136
TEST(MPSCRingBufferTest, EmplaceBasicType) {
82137
int value;
83138
RingBuffer::MPSCRingBuffer<int> buffer(2);

0 commit comments

Comments
 (0)