蓝色天空.知识 · 实践 · 创造
我的收藏
在线代码实验室

实测 C++17 auto 类型推导、static 持久计数、extern/mutable 及 pthread 下的 thread_local 线程隔离

CPP本地离线 Docker 容器(mstorsjo/llvm-mingw:20260616,G++ 13.3.0 C++17 linux/amd64,--network none)。explore-cpp-storage-classes.cpp
explore-cpp-storage-classes.cpp147 行
#include <cstdio>
#include <iostream>
#include <string>
#include <thread>
#include <type_traits>
#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"; }
std::string jint(long long v) { return std::to_string(v); }
std::string juint(unsigned long long v) { return std::to_string(v); }

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

static int file_count = 10;
extern int shared_ext_count;
int shared_ext_count = 5;

static int step_static() {
    static int i = 5;
    ++i;
    return i;
}

class MutableDemo {
public:
    explicit MutableDemo(int v) : value_(v), read_hits_(0) {}
    int get() const {
        ++read_hits_;
        return value_;
    }
    int hits() const { return read_hits_; }
private:
    int value_;
    mutable int read_hits_;
};

thread_local int tls_val = 0;

int main() {
    std::vector<lab::Case> cases;

    // Case 1: C++11/17 auto type deduction (no legacy auto storage class)
    auto f = 3.14;
    auto s("hello");
    auto* z = new auto(9);
    bool auto_ok = std::is_same_v<decltype(f), double> &&
                   std::is_same_v<decltype(s), const char*> &&
                   std::is_same_v<decltype(z), int*> && (*z == 9);
    delete z;
    cases.push_back({
        "auto_type_deduction_cpp17",
        lab::jbool(true),
        lab::jbool(auto_ok),
        auto_ok
    });

    // Case 2: static local persists across 10 calls; file-scope static decrements
    int last_i = 0;
    while (file_count--) {
        last_i = step_static();
    }
    bool static_ok = (last_i == 15) && (file_count == -1);
    cases.push_back({
        "static_local_and_file_scope",
        lab::jint(15),
        lab::jint(last_i),
        static_ok
    });

    // Case 3: extern linkage + mutable member modified inside const method
    extern int shared_ext_count;
    const MutableDemo md(42);
    int v1 = md.get();
    int v2 = md.get();
    bool em_ok = (shared_ext_count == 5) && (v1 == 42) && (v2 == 42) && (md.hits() == 2);
    cases.push_back({
        "extern_and_mutable_const_method",
        lab::jbool(true),
        lab::jbool(em_ok),
        em_ok
    });

    // Case 4: thread_local gives independent storage per std::thread
    int observed1 = 0, observed2 = 0;
    tls_val = 99;
    std::thread t1([&]() { tls_val = 101; observed1 = tls_val; });
    std::thread t2([&]() { tls_val = 202; observed2 = tls_val; });
    t1.join();
    t2.join();
    bool tls_ok = (tls_val == 99) && (observed1 == 101) && (observed2 == 202);
    cases.push_back({
        "thread_local_isolation_pthread",
        lab::esc("main=99,t1=101,t2=202"),
        lab::esc("main=" + std::to_string(tls_val) + ",t1=" + std::to_string(observed1) + ",t2=" + std::to_string(observed2)),
        tls_ok
    });

    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-storage-classes-lab/explore-cpp-storage-classes.cpp

本地运行步骤快速上手
  1. 点击上方“保存源码”保存为 explore-cpp-storage-classes.cpp,或下载右侧课程工程包。
  2. 确认本地已准备 本地离线 Docker 容器(mstorsjo/llvm-mingw:20260616,G++ 13.3.0 C++17 linux/amd64,--network none)。。
  3. 按对应章节命令执行,结合正文核对实际输出。
附件与关联工程包下载

下载课程关联工程包:C++

课程关联工程包(登记关联99章) · 含当前章节或实例 · mbskycom202612a2307.zip

下载课程关联工程包:C++

课程关联工程包(登记关联1章) · 含当前章节或实例 · mbskycom202612a2396.zip
SITE SEARCH

搜索教程、工具与资讯

热门搜索

JavaScriptJSON格式化正则表达式AI 编程AI 翻译AI导航专题标签
Esc 关闭

欢迎登录蓝色天空网

使用会员账号,继续你的学习