#include <atomic>
#include <cstdio>
#include <iostream>
#include <string>
#include <thread>
#include <vector>
namespace lab {
struct Case {
std::string name;
std::string expected_json;
std::string actual_json;
bool passed;
};
std::string esc(const std::string& s) {
std::string out = "\"";
for (unsigned char c : s) {
switch (c) {
case '\"': out += "\\\""; break;
case '\\': out += "\\\\"; break;
case '\b': out += "\\b"; break;
case '\f': out += "\\f"; break;
case '\n': out += "\\n"; break;
case '\r': out += "\\r"; break;
case '\t': out += "\\t"; break;
default:
if (c < 0x20) {
char buf[8];
std::snprintf(buf, sizeof(buf), "\\u%04x", c);
out += buf;
} else {
out += static_cast<char>(c);
}
}
}
out += "\"";
return out;
}
std::string jbool(bool v) { return v ? "true" : "false"; }
void emit(const std::vector<Case>& cases) {
std::cout << "{\"runtime\": \"G++ C++17 (mstorsjo/llvm-mingw:20260616)\", \"cases\": [";
for (std::size_t i = 0; i < cases.size(); ++i) {
if (i) std::cout << ", ";
std::cout << "{\"name\": " << esc(cases[i].name)
<< ", \"expected\": " << cases[i].expected_json
<< ", \"actual\": " << cases[i].actual_json
<< ", \"passed\": " << jbool(cases[i].passed) << "}";
}
std::cout << "]}\n";
}
} // namespace lab
int main() {
std::vector<lab::Case> cases;
// Case 1: load, store, exchange across integral/bool/double atomic types
std::atomic<int> ai(10);
ai.store(25);
int loaded = ai.load();
int prev = ai.exchange(40);
int after = ai.load();
std::atomic<bool> ab(false);
ab.store(true);
std::atomic<double> ad(1.5);
double prev_d = ad.exchange(3.25);
bool ok1 = (loaded == 25) && (prev == 25) && (after == 40)
&& ab.load() && (prev_d == 1.5) && (ad.load() == 3.25);
cases.push_back({
"atomic_load_store_exchange",
lab::jbool(true),
lab::jbool(ok1),
ok1
});
// Case 2: compare_exchange_strong and compare_exchange_weak loop
std::atomic<int> cas_var(100);
int expected_wrong = 50;
bool strong_fail = cas_var.compare_exchange_strong(expected_wrong, 200);
// expected_wrong is now updated to 100
bool strong_ok = cas_var.compare_exchange_strong(expected_wrong, 200);
// CAS loop using compare_exchange_weak to add 50
int cur = cas_var.load();
while (!cas_var.compare_exchange_weak(cur, cur + 50)) {
// retry on spurious failure or contention
}
std::string act2 = "fail=" + std::to_string(strong_fail ? 1 : 0)
+ ";updatedExp=" + std::to_string(expected_wrong)
+ ";ok=" + std::to_string(strong_ok ? 1 : 0)
+ ";final=" + std::to_string(cas_var.load());
std::string exp2 = "fail=0;updatedExp=100;ok=1;final=250";
cases.push_back({
"atomic_compare_exchange_weak_strong",
lab::esc(exp2),
lab::esc(act2),
act2 == exp2
});
// Case 3: multithreaded counter with fetch_add(1, memory_order_relaxed)
std::atomic<int> counter(0);
auto inc = [&]() {
for (int i = 0; i < 10000; ++i) {
counter.fetch_add(1, std::memory_order_relaxed);
}
};
std::thread t1(inc);
std::thread t2(inc);
t1.join();
t2.join();
int total = counter.load();
cases.push_back({
"atomic_multithread_counter_relaxed",
std::to_string(20000),
std::to_string(total),
total == 20000
});
// Case 4: release-acquire synchronization guaranteeing non-atomic payload visibility
int payload = 0;
std::atomic<bool> ready(false);
int observed = -1;
std::thread writer([&]() {
payload = 42;
ready.store(true, std::memory_order_release);
});
std::thread reader([&]() {
while (!ready.load(std::memory_order_acquire)) {
std::this_thread::yield();
}
observed = payload;
});
writer.join();
reader.join();
cases.push_back({
"atomic_release_acquire_visibility",
std::to_string(42),
std::to_string(observed),
observed == 42
});
lab::emit(cases);
return 0;
}
本例需在 本地离线 Docker 容器(mstorsjo/llvm-mingw:20260616,G++ 13.3.0 C++17 linux/amd64,--network none)。 中运行,请复制源码、保存文件或下载工程包后在本地执行验证。
运行说明与环境独立工程
运行环境:本地离线 Docker 容器(mstorsjo/llvm-mingw:20260616,G++ 13.3.0 C++17 linux/amd64,--network none)。
入口文件:projects/cplusplus-cpp-libs-atomic-lab/explore-cpp-libs-atomic.cpp
本地运行步骤快速上手
- 点击上方“保存源码”保存为
explore-cpp-libs-atomic.cpp,或下载右侧课程工程包。 - 确认本地已准备 本地离线 Docker 容器(mstorsjo/llvm-mingw:20260616,G++ 13.3.0 C++17 linux/amd64,--network none)。。
- 按对应章节命令执行,结合正文核对实际输出。
附件与关联工程包下载
课程关联工程包(登记关联99章) · 含当前章节或实例 · mbskycom202612a2307.zip
课程关联工程包(登记关联1章) · 含当前章节或实例 · mbskycom202612a2331.zip