赞
踩
编写测试程序的最终版本如下,此版本是经过了反复调整,力求增大复现问题的概率。
#include
#include
#include<unistd.h>
#define TAG “MY_TEST”
#define MY_DEBUG printf // add your platform timestamp threadid
class Machine
{
public:
Machine(int year_) : year(year_) {
MY_DEBUG(“before construct Machine:%p, thread:%d, year:%d”, this, year_, year);
MY_DEBUG(“after construct Machine:%p, thread:%d, year:%d”, this, year_, year);
}
~Machine() {
MY_DEBUG(“before destruct, year:%d”, year);
year = -1;
MY_DEBUG(“after destruct, set -1”); // -1 as invalid static data.
}
int GetData() const {
return year;
}
int year;
};
static const Machine golbalMachine(100); // 100 as global static valid data
const Machine &GetMachine(int index)
{
const static Machine machine(index);
MY_DEBUG(“return object for thread: %d. machine:%p, year:%d, globalMachine:%p, globalYear:%d”, index, &machine, machine.GetData(), &golbalMachine, golbalMachine.GetData());
return machine;
}
void CheckStaticVariable() {
std::thread t1(GetMachine, 1);
std::thread t2(GetMachine, 2);
std::thread t3(GetMachine, 3);
MY_DEBUG(“t1 detach”);
t1.detach();
MY_DEBUG(“t2 detach”);
t2.detach();
MY_DEBUG(“t3 detach”);
t3.detach();
// MY_DEBUG(“t1 join”);
// t1.join();
// MY_DEBUG(“t2 join”);
// t2.join();
// MY_DEBUG(“t3 join”);
// t3.join();
}
int main() {
MY_DEBUG(“start main\n”);
CheckStaticVariable();
exit(0); // in order to auto complete ios app main thread.
return 0;
}
linux 平台实测
在Ubuntu机器上用GCC和CLANG两种编译器运行测试。
Description: Ubuntu 20.04.2 LTS
Release: 20.04
g++ (Ubuntu 9.3.0-17ubuntu1~20.04) 9.3.0
clang version 10.0.1
Target: x86_64-unknown-linux-gnu
Thread model: posix
使用GCC编译器,编译运行上述程序, 在shell中持续循环运行多次,静态变量析构都发生在线程执行完成之后。得到如下结果。GetMachine函数得到的全局、局部静态变量数值都是大于0的有效值,并非-1.
使用fno-threadsafe-statics
制造不安全场景, 大规模循环执行后,复现了子线程读取到错误数据的case,此局部静态变量已经析构。由于实验的是简单数据类型, 并没有crash。如果是访问析构后的复杂对象,则可能crash。 而且从多线程发生重复竞争构造同一个静态对象,重复析构同一个静态对象。
同时发现,在大量循环后,全局静态变量始终在子线程访问结束后才析构,与gcc 编译器的说明是对应的,也就是说这个特性一直打开,不受fno-threadsafe-statics
影响,此选项只影响局部静态变量。
使用 clang编译器,编译运行上述程序, 在shell中持续循环运行多次,静态变量析构都发生在线程执行完成之后。多线程安全。
使用fno-threadsafe-statics
制造不安全场景, 大规模循环执行后,可复现了子线程读取到错误数据的case,此局部静态变量已经析构。
为了进一步查看区别和原因,使用GCC编译器编出汇编代码继续分析,使用fno-threadsafe-statics
选项得到不安全的汇编代码,对应下图左侧,右侧为静态变量多线程安全代码。将符号demangle后分析差异。
g++ -std=c++11 -pthread -S static_thread.cpp -o static_thread_gcc_x64.S
g++ -std=c++11 -pthread -fno-threadsafe-statics -S static_thread.cpp -o static_thread_gcc_x64_unsafe.S
threadstatic# c++filt _ZGVZ10GetMachineiE7machine
guard variable for GetMachine(int)::machine
threadstatic# c++filt _ZZ10GetMachineiE7machine
GetMachine(int)::machine
threadstatic# c++filt _ZN7MachineC1Ei
Machine::Machine(int)
/threadstatic# c++filt _ZN7MachineD1Ev
Machine::~Machine()
_ZZ10GetMachineiE7machine
为 GetMachine(int)::machine
函数调用, _ZN7MachineC1Ei
为Machine类的构造函数。线程安全版本在调用GetMachine时先通过__cxa_guard_acquire
获取了guard锁,call _ZN7MachineC1Ei
调用Machine类构造局部静态对象之后,再用__ __cxa_guard_abort
和 __cxa_guard_release
释放锁。最后把析构函数_ZN7MachineD1Ev
用__cxa_atexit
注册到程序退出函数表中。
从gcc说明文档也可以看到这个信息:
// The actual code emitted by GCC to call a local static variable’s constructor looks something like this:
static guard;
if (!guard.first_byte)
{
if (__cxa_guard_acquire (&guard))
{
bool flag = false;
try
{
// Do initialization.
__cxa_guard_release (&guard);
flag = true;
// Register variable for destruction at end of program.
}
catch
{
if (!flag)
{
__cxa_guard_abort (&guard);
}
}
}
}
在实测部分,大量循环后,全局静态变量始终在子线程访问结束后才析构,说这个特性一直打开,不受fno-threadsafe-statics
影响,所以汇编代码无法对比出差异,受时间限制暂未去找实现的原理。
Apple 平台实测
Clang 12.0.5
保持xcode工程的 Statics are Thread-Safe
选项打开。设定optimization level为-O0
,无优化。
手动在iphone XS上运行测试代码,出现下图问题,子线程访问到-1数据时,主线程在调用_exit
函数退出,触发_cxa_atexit中注册过的静态变量析构函数。由于实验的是简单数据类型, 并没有crash。如果是访问析构后的复杂对象,则可能crash,正好对应线上crash的场景。
// 线上crash堆栈
Thread 0:
1 libsystem_c.dylib 0x0000000197db7064 __cxa_finalize_ranges :416 (in libsystem_c.dylib)
2 libsystem_c.dylib 0x0000000197db73a0 _exit :28 (in libsystem_c.dylib)
3 UIKitCore 0x000000019c2301a4 -[UIApplication _terminateWithStatus:] :508 (in UIKitCore)
复制
而linux平台只有关闭线程安全选项fno-threadsafe-statics
后才会出现。正常编译下静态变量构造和析构都是安全的。说明此clang+ios并未实现完整的Dynamic Initialization and Destruction with Concurrency
.
IOS MNN crash的根源在于Apple clang编译器和运行时库未实现静态变量析构的多线程安全。
Apple clang version 12.0.5 (clang-1205.0.22.11)
Target: x86_64-apple-darwin20.3.0
Thread model: posix
在mac下使用Apple clang编译,静态变量线程安全选项默认打开情况下,仍然可以复现此问题, 和ios的情况一致。
clang++ -std=c++11 -pthread static_thread.cpp -o static_thread_clang_mac
android平台实测
Android (7019983 based on r365631c3) clang version 9.0.9 (https://android.googlesource.com/toolchain/llvm-project a2a1e703c0edb03ba29944e529ccbf457742737b) (based on LLVM 9.0.9svn)
Target: x86_64-apple-darwin20.3.0
Thread model: posix
android NDK r21e的clang版本为9.09, 大于了c++11此特性标注的clang 2.9, 编译测试代码,执行可以看到构造过程仍然保证只构造一次, thread1 线程号15623, GetMachine取到的局部和全局静态变量都是-1,因为局部和全局静态变量析构函数已经早于此线程调用GetMachine执行了。此版本析构过程线程不安全。说明此clang+ndk库并未实现完整的 Dynamic Initialization and Destruction with Concurrency
.
我手头的ndk版本不高,哪位同学如果有更高版本,方便的话可编译运行一下此测试程序。
Using built-in specs.
COLLECT_GCC=aarch64-linux-gnu-g++
COLLECT_LTO_WRAPPER=/usr/lib/gcc-cross/aarch64-linux-gnu/9/lto-wrapper
Target: aarch64-linux-gnu
Thread model: posix
自我介绍一下,小编13年上海交大毕业,曾经在小公司待过,也去过华为、OPPO等大厂,18年进入阿里一直到现在。
深知大多数Java工程师,想要提升技能,往往是自己摸索成长或者是报班学习,但对于培训机构动则几千的学费,着实压力不小。自己不成体系的自学效果低效又漫长,而且极易碰到天花板技术停滞不前!
因此收集整理了一份《2024年Java开发全套学习资料》,初衷也很简单,就是希望能够帮助到想自学提升又不知道该从何学起的朋友,同时减轻大家的负担。
既有适合小白学习的零基础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,基本涵盖了95%以上Java开发知识点,真正体系化!
由于文件比较大,这里只是将部分目录大纲截图出来,每个节点里面都包含大厂面经、学习笔记、源码讲义、实战项目、讲解视频,并且后续会持续更新
如果你觉得这些内容对你有帮助,可以添加V获取:vip1024b (备注Java)
本人面试腾讯,阿里,百度等企业总结下来的面试经历,都是真实的,分享给大家!
准确的说这里又分为两部分:
Java刷题:此份文档详细记录了千道面试题与详解;
笔记、源码讲义、实战项目、讲解视频,并且后续会持续更新**
如果你觉得这些内容对你有帮助,可以添加V获取:vip1024b (备注Java)
[外链图片转存中…(img-PlLzva4A-1711616821545)]
本人面试腾讯,阿里,百度等企业总结下来的面试经历,都是真实的,分享给大家!
[外链图片转存中…(img-s9zjJIjM-1711616821546)]
[外链图片转存中…(img-a2nBP61M-1711616821546)]
[外链图片转存中…(img-EGozi7UG-1711616821547)]
[外链图片转存中…(img-DigoKkRB-1711616821547)]
准确的说这里又分为两部分:
Java刷题:此份文档详细记录了千道面试题与详解;
[外链图片转存中…(img-CwsOzBYD-1711616821547)]
[外链图片转存中…(img-9zTykavM-1711616821548)]
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。