#include <chrono>
#include <condition_variable>
#include <cstdio>
#include <future>
#include <iostream>
#include <mutex>
#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: std::thread creation, get_id, joinable, join, detach
std::thread::id main_id = std::this_thread::get_id();
std::thread::id id1{}, id2{};
std::thread t1([&]() {
std::this_thread::sleep_for(std::chrono::milliseconds(5));
id1 = std::this_thread::get_id();
});
std::thread t2([&]() {
id2 = std::this_thread::get_id();
});
bool j1_before = t1.joinable();
t1.join();
t2.join();
bool j1_after = t1.joinable();
// Safe detached thread synchronized via promise/future before main exits
std::promise<int> detach_done;
auto detach_fut = detach_done.get_future();
std::thread td([p = std::move(detach_done)]() mutable {
p.set_value(77);
});
bool jd_before = td.joinable();
td.detach();
bool jd_after = td.joinable();
int detach_val = detach_fut.get();
bool ok1 = j1_before && !j1_after && jd_before && !jd_after
&& (id1 != main_id) && (id2 != main_id) && (detach_val == 77);
cases.push_back({
"thread_lifecycle_and_ids",
lab::jbool(true),
lab::jbool(ok1),
ok1
});
// Case 2: synchronized parallel sum using mutex + lock_guard (no data race)
std::mutex sum_mtx;
int sum = 0;
auto add_safe = [&](int a, int b) {
std::lock_guard<std::mutex> lock(sum_mtx);
sum += a + b;
};
std::thread s1(add_safe, 5, 10);
std::thread s2(add_safe, 5, 10);
s1.join();
s2.join();
cases.push_back({
"thread_synchronized_parallel_sum",
std::to_string(30),
std::to_string(sum),
sum == 30
});
// Case 3: unique_lock manual unlock + condition_variable notify_all
std::mutex cv_mtx;
std::condition_variable cv;
bool ready = false;
int woken = 0;
auto waiter = [&]() {
std::unique_lock<std::mutex> lock(cv_mtx);
cv.wait(lock, [&]() { return ready; });
++woken;
lock.unlock();
};
std::thread w1(waiter);
std::thread w2(waiter);
{
std::unique_lock<std::mutex> lock(cv_mtx);
ready = true;
}
cv.notify_all();
w1.join();
w2.join();
cases.push_back({
"thread_unique_lock_and_cv_notify",
std::to_string(2),
std::to_string(woken),
woken == 2
});
// Case 4: promise/future and std::async square calculation
std::promise<int> sq_prom;
std::future<int> sq_fut = sq_prom.get_future();
std::thread tp([](std::promise<int>&& p, int x) {
p.set_value(x * x);
}, std::move(sq_prom), 5);
int v_prom = sq_fut.get();
tp.join();
std::future<int> a_fut = std::async(std::launch::async, [](int x) { return x * x; }, 5);
int v_async = a_fut.get();
std::string act4 = "prom=" + std::to_string(v_prom) + ";async=" + std::to_string(v_async);
std::string exp4 = "prom=25;async=25";
cases.push_back({
"thread_promise_future_and_async",
lab::esc(exp4),
lab::esc(act4),
act4 == exp4
});
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-thread-lab/explore-cpp-libs-thread.cpp
本地运行步骤快速上手
- 点击上方“保存源码”保存为
explore-cpp-libs-thread.cpp,或下载右侧课程工程包。 - 确认本地已准备 本地离线 Docker 容器(mstorsjo/llvm-mingw:20260616,G++ 13.3.0 C++17 linux/amd64,--network none)。。
- 按对应章节命令执行,结合正文核对实际输出。
附件与关联工程包下载
课程关联工程包(登记关联99章) · 含当前章节或实例 · mbskycom202612a2307.zip
课程关联工程包(登记关联1章) · 含当前章节或实例 · mbskycom202612a2372.zip