C/C++ 11 thread_local 注意事项

本文解析了C++11中thread_local的原理,强调了其依赖操作系统TLS存储,介绍了Windows API中的相关API。讨论了内存泄露问题及C++11编译器如何在线程结束时自动清理thread_local变量,以及在不同线程环境下的适用性和注意事项。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

thread_local 等价于 “static thread_local”,即局部线程存储(TLS)技术,注意:此(TLS)并非某 SSL/TLS 安全传输层协议,那个东西哈,乱入要有一个限度。

先贴一段代码:

class Foo {
public:
    ~Foo() {
        printf("close\r\n");
    }
    void say() {
        printf("say\r\n");
    }
};    

    std::thread([] {
        thread_local Foo foo;
        foo.say();
    }).detach();

此处以 Windows VC++ 为例:(GCC实际上也差不多)

我们先看看:foo.say() 是如何被执行的。

01094A7C 8B 0D 70 B3 59 01    mov         ecx,dword ptr [_tls_index (0159B370h)]  
01094A82 64 8B 15 2C 00 00 00 mov         edx,dword ptr fs:[2Ch]  
01094A89 8B 0C 8A             mov         ecx,dword ptr [edx+ecx*4]  
01094A8C 89 81 14 01 00 00    mov         dword ptr [ecx+114h],eax  
01094A92 68 18 E1 F5 00       push        offset `<lambda_8db51befb6b9322d4d50df9ac98a9cf6>::operator()'::`2'::`dynamic atexit destructor for 'foo'' (0F5E118h)  
01094A97 E8 61 9D EC FF       call        ___tlregdtor (0F5E7FDh)  
01094A9C 83 C4 04             add         esp,4  
   284:         foo.say();

可以看到它依赖于操作系统提供 Thread Local Store (TLS)线程存储机制,Windows API提供以下几个API:

TlsAlloc、TlsGetValue、TlsSetValue、TlsFree,分别对应几种不同的行为,每个线程在分配时操作系统内核都会为线程单独分配一个“TLS索引数组缓冲区”,注意这是有限大小,所以不可能允许开发人员无限制的分配大量的TLS变量。

而在 C++11 上面,thread_local 语法糖是存在内存泄露问题的,但按照标准化套路实现不会存在这个问题,举例:

我们通过 CreateThread 函数创建一个新的工作线程,并在该线程内部使用 thread_local 声明一个变量,那么就会产生泄露,因为没有地方调用 C/C++ 11 编译器内部实现的 “thread_local” 变量资源回收函数,而该函数是非公开的(编译器,编译期实现)。

而C/C++11提供的线程变量资源回收的契机是在 “thread_start(std::thread)”线程执行完毕以后,清理这些由 thread_local 语法糖声明持有的TLS线程存储变量资源。

 一个全新的问题?如果C/C++11编译的 main 入口点执行函数(即STA线程模式下的主线程)执行完毕会不会执行 “TLS线程存储变量资源”呢?答案是可以的。

现代的C/C++11编译器,main 程序入口点函数已经不再是真正意义上的系统执行PE文件的可执行程序入口点函数了,在进入用户 main 函数以前现代 C/C++ 编译器会执行大量的由编译器生成的 “代码”,一般为初始化 “static global、common global” 定义的C++结构或类的分配(自然会调用构造函数),同理在 main 函数结束以后会执行相应的析构行为,而由C++11提供的 thread_local 实现的线程存储变量,自然也在析构的范畴。

所以:thread_local 语法糖实现的线程存储变量,不允许在 std::thread 标准库线程模板以外的方式创建的线程上面使用,这类情况则需要人工手动管理TLS线程存储(如果非要用)。

补充:但是CreateThread创建的线程上面仍旧可以使用 thread_local 进行释放,这种需要捕获系统释放TLS变量的堆栈,自己在C++中声明由编译器实现的线程回收函数,只要调用协议没什么问题,那么就可以让C/C++程序连接器连接上该函数,在线程结束的时候手动去调这个函数就可以回收了,只是不是很建议这样操作了,类似的操作也有替换C++编译器默认实现的 new,这种呢,依赖于对运算符的重载来实现,但缺点是只能是当前编译工程代码内可以被连接上,如其它编译为DLL的没什么用。

linux安装python依赖:[root@VM-16-5-centos noon]# pip3.6 install sqlalchemy WARNING: Running pip install with root privileges is generally not a good idea. Try `pip3.6 install --user` instead. Collecting sqlalchemy Downloading http://mirrors.tencentyun.com/pypi/packages/5e/50/f63ff7811a8d3367a2c7fae6095f08da20e64e09762e4da1bf05706aefb1/SQLAlchemy-1.4.54-cp36-cp36m-manylinux1_x86_64.manylinux2010_x86_64.manylinux_2_12_x86_64.manylinux_2_5_x86_64.manylinux_2_17_x86_64.manylinux2014_x86_64.whl (1.6MB) 100% |████████████████████████████████| 1.6MB 1.8MB/s Collecting greenlet!=0.4.17; python_version >= "3" and (platform_machine == "aarch64" or (platform_machine == "ppc64le" or (platform_machine == "x86_64" or (platform_machine == "amd64" or (platform_machine == "AMD64" or (platform_machine == "win32" or platform_machine == "WIN32")))))) (from sqlalchemy) Downloading http://mirrors.tencentyun.com/pypi/packages/1e/1e/632e55a04d732c8184201238d911207682b119c35cecbb9a573a6c566731/greenlet-2.0.2.tar.gz (164kB) 100% |████████████████████████████████| 174kB 920kB/s Collecting importlib-metadata; python_version < "3.8" (from sqlalchemy) Downloading http://mirrors.tencentyun.com/pypi/packages/a0/a1/b153a0a4caf7a7e3f15c2cd56c7702e2cf3d89b1b359d1f1c5e59d68f4ce/importlib_metadata-4.8.3-py3-none-any.whl Collecting zipp>=0.5 (from importlib-metadata; python_version < "3.8"->sqlalchemy) Downloading http://mirrors.tencentyun.com/pypi/packages/bd/df/d4a4974a3e3957fd1c1fa3082366d7fff6e428ddb55f074bf64876f8e8ad/zipp-3.6.0-py3-none-any.whl Requirement already satisfied: typing-extensions>=3.6.4; python_version < "3.8" in /usr/local/lib/python3.6/site-packages (from importlib-metadata; python_version < "3.8"->sqlalchemy) Installing collected packages: greenlet, zipp, importlib-metadata, sqlalchemy Running setup.py install for greenlet ... error Complete output from command /usr/bin/python3 -u -c "import setuptools, tokenize;__file__='/tmp/pip-build-xwpa9djj/greenlet/setup.py';f=getattr(tokenize, 'open', open)(__file__);code=f.read().replace('\r\n', '\n');f.close();exec(compile(code, __file__, 'exec'))" install --record /tmp/pip-5rl6bxpe-record/install-record.txt --single-version-externally-managed --compile: running install running build running build_py creating build creating build/lib.linux-x86_64-3.6 creating build/lib.linux-x86_64-3.6/greenlet copying src/greenlet/__init__.py -> build/lib.linux-x86_64-3.6/greenlet creating build/lib.linux-x86_64-3.6/greenlet/platform copying src/greenlet/platform/__init__.py -> build/lib.linux-x86_64-3.6/greenlet/platform creating build/lib.linux-x86_64-3.6/greenlet/tests copying src/greenlet/tests/test_generator.py -> build/lib.linux-x86_64-3.6/greenlet/tests copying src/greenlet/tests/test_stack_saved.py -> build/lib.linux-x86_64-3.6/greenlet/tests copying src/greenlet/tests/test_contextvars.py -> build/lib.linux-x86_64-3.6/greenlet/tests copying src/greenlet/tests/test_greenlet.py -> build/lib.linux-x86_64-3.6/greenlet/tests copying src/greenlet/tests/test_cpp.py -> build/lib.linux-x86_64-3.6/greenlet/tests copying src/greenlet/tests/test_gc.py -> build/lib.linux-x86_64-3.6/greenlet/tests copying src/greenlet/tests/test_extension_interface.py -> build/lib.linux-x86_64-3.6/greenlet/tests copying src/greenlet/tests/test_version.py -> build/lib.linux-x86_64-3.6/greenlet/tests copying src/greenlet/tests/test_throw.py -> build/lib.linux-x86_64-3.6/greenlet/tests copying src/greenlet/tests/test_greenlet_trash.py -> build/lib.linux-x86_64-3.6/greenlet/tests copying src/greenlet/tests/test_weakref.py -> build/lib.linux-x86_64-3.6/greenlet/tests copying src/greenlet/tests/test_tracing.py -> build/lib.linux-x86_64-3.6/greenlet/tests copying src/greenlet/tests/__init__.py -> build/lib.linux-x86_64-3.6/greenlet/tests copying src/greenlet/tests/leakcheck.py -> build/lib.linux-x86_64-3.6/greenlet/tests copying src/greenlet/tests/test_generator_nested.py -> build/lib.linux-x86_64-3.6/greenlet/tests copying src/greenlet/tests/test_leaks.py -> build/lib.linux-x86_64-3.6/greenlet/tests running egg_info writing src/greenlet.egg-info/PKG-INFO writing dependency_links to src/greenlet.egg-info/dependency_links.txt writing requirements to src/greenlet.egg-info/requires.txt writing top-level names to src/greenlet.egg-info/top_level.txt reading manifest file 'src/greenlet.egg-info/SOURCES.txt' reading manifest template 'MANIFEST.in' warning: no previously-included files found matching 'benchmarks/*.json' no previously-included directories found matching 'docs/_build' warning: no files found matching '*.py' under directory 'appveyor' warning: no previously-included files matching '*.pyc' found anywhere in distribution warning: no previously-included files matching '*.pyd' found anywhere in distribution warning: no previously-included files matching '*.so' found anywhere in distribution warning: no previously-included files matching '.coverage' found anywhere in distribution writing manifest file 'src/greenlet.egg-info/SOURCES.txt' copying src/greenlet/greenlet.cpp -> build/lib.linux-x86_64-3.6/greenlet copying src/greenlet/greenlet.h -> build/lib.linux-x86_64-3.6/greenlet copying src/greenlet/greenlet_allocator.hpp -> build/lib.linux-x86_64-3.6/greenlet copying src/greenlet/greenlet_compiler_compat.hpp -> build/lib.linux-x86_64-3.6/greenlet copying src/greenlet/greenlet_cpython_compat.hpp -> build/lib.linux-x86_64-3.6/greenlet copying src/greenlet/greenlet_exceptions.hpp -> build/lib.linux-x86_64-3.6/greenlet copying src/greenlet/greenlet_greenlet.hpp -> build/lib.linux-x86_64-3.6/greenlet copying src/greenlet/greenlet_internal.hpp -> build/lib.linux-x86_64-3.6/greenlet copying src/greenlet/greenlet_refs.hpp -> build/lib.linux-x86_64-3.6/greenlet copying src/greenlet/greenlet_slp_switch.hpp -> build/lib.linux-x86_64-3.6/greenlet copying src/greenlet/greenlet_thread_state.hpp -> build/lib.linux-x86_64-3.6/greenlet copying src/greenlet/greenlet_thread_state_dict_cleanup.hpp -> build/lib.linux-x86_64-3.6/greenlet copying src/greenlet/greenlet_thread_support.hpp -> build/lib.linux-x86_64-3.6/greenlet copying src/greenlet/slp_platformselect.h -> build/lib.linux-x86_64-3.6/greenlet copying src/greenlet/platform/setup_switch_x64_masm.cmd -> build/lib.linux-x86_64-3.6/greenlet/platform copying src/greenlet/platform/switch_aarch64_gcc.h -> build/lib.linux-x86_64-3.6/greenlet/platform copying src/greenlet/platform/switch_alpha_unix.h -> build/lib.linux-x86_64-3.6/greenlet/platform copying src/greenlet/platform/switch_amd64_unix.h -> build/lib.linux-x86_64-3.6/greenlet/platform copying src/greenlet/platform/switch_arm32_gcc.h -> build/lib.linux-x86_64-3.6/greenlet/platform copying src/greenlet/platform/switch_arm32_ios.h -> build/lib.linux-x86_64-3.6/greenlet/platform copying src/greenlet/platform/switch_arm64_masm.asm -> build/lib.linux-x86_64-3.6/greenlet/platform copying src/greenlet/platform/switch_arm64_masm.obj -> build/lib.linux-x86_64-3.6/greenlet/platform copying src/greenlet/platform/switch_arm64_msvc.h -> build/lib.linux-x86_64-3.6/greenlet/platform copying src/greenlet/platform/switch_csky_gcc.h -> build/lib.linux-x86_64-3.6/greenlet/platform copying src/greenlet/platform/switch_m68k_gcc.h -> build/lib.linux-x86_64-3.6/greenlet/platform copying src/greenlet/platform/switch_mips_unix.h -> build/lib.linux-x86_64-3.6/greenlet/platform copying src/greenlet/platform/switch_ppc64_aix.h -> build/lib.linux-x86_64-3.6/greenlet/platform copying src/greenlet/platform/switch_ppc64_linux.h -> build/lib.linux-x86_64-3.6/greenlet/platform copying src/greenlet/platform/switch_ppc_aix.h -> build/lib.linux-x86_64-3.6/greenlet/platform copying src/greenlet/platform/switch_ppc_linux.h -> build/lib.linux-x86_64-3.6/greenlet/platform copying src/greenlet/platform/switch_ppc_macosx.h -> build/lib.linux-x86_64-3.6/greenlet/platform copying src/greenlet/platform/switch_ppc_unix.h -> build/lib.linux-x86_64-3.6/greenlet/platform copying src/greenlet/platform/switch_riscv_unix.h -> build/lib.linux-x86_64-3.6/greenlet/platform copying src/greenlet/platform/switch_s390_unix.h -> build/lib.linux-x86_64-3.6/greenlet/platform copying src/greenlet/platform/switch_sparc_sun_gcc.h -> build/lib.linux-x86_64-3.6/greenlet/platform copying src/greenlet/platform/switch_x32_unix.h -> build/lib.linux-x86_64-3.6/greenlet/platform copying src/greenlet/platform/switch_x64_masm.asm -> build/lib.linux-x86_64-3.6/greenlet/platform copying src/greenlet/platform/switch_x64_masm.obj -> build/lib.linux-x86_64-3.6/greenlet/platform copying src/greenlet/platform/switch_x64_msvc.h -> build/lib.linux-x86_64-3.6/greenlet/platform copying src/greenlet/platform/switch_x86_msvc.h -> build/lib.linux-x86_64-3.6/greenlet/platform copying src/greenlet/platform/switch_x86_unix.h -> build/lib.linux-x86_64-3.6/greenlet/platform copying src/greenlet/tests/_test_extension.c -> build/lib.linux-x86_64-3.6/greenlet/tests copying src/greenlet/tests/_test_extension_cpp.cpp -> build/lib.linux-x86_64-3.6/greenlet/tests running build_ext building 'greenlet._greenlet' extension creating build/temp.linux-x86_64-3.6 creating build/temp.linux-x86_64-3.6/src creating build/temp.linux-x86_64-3.6/src/greenlet gcc -pthread -Wno-unused-result -Wsign-compare -DNDEBUG -O2 -g -pipe -Wall -Wp,-D_FORTIFY_SOURCE=2 -fexceptions -fstack-protector-strong --param=ssp-buffer-size=4 -grecord-gcc-switches -m64 -mtune=generic -D_GNU_SOURCE -fPIC -fwrapv -fPIC -I/usr/include/python3.6m -c src/greenlet/greenlet.cpp -o build/temp.linux-x86_64-3.6/src/greenlet/greenlet.o gcc: error trying to exec 'cc1plus': execvp: 没有那个文件或目录 error: command 'gcc' failed with exit status 1 ---------------------------------------- Command "/usr/bin/python3 -u -c "import setuptools, tokenize;__file__='/tmp/pip-build-xwpa9djj/greenlet/setup.py';f=getattr(tokenize, 'open', open)(__file__);code=f.read().replace('\r\n', '\n');f.close();exec(compile(code, __file__, 'exec'))" install --record /tmp/pip-5rl6bxpe-record/install-record.txt --single-version-externally-managed --compile" failed with error code 1 in /tmp/pip-build-xwpa9djj/greenlet/ [root@VM-16-5-centos noon]# python3.6 getSalerInfo3.py Traceback (most recent call last): File "getSalerInfo3.py", line 13, in <module> from sqlalchemy import exc, create_engine, text ModuleNotFoundError: No module named 'sqlalchemy' [root@VM-16-5-centos noon]#报错
06-19
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值