c++的静态类型变量初始化是原子操作吗?下面的单例有没有问题?请大神指点一二

深度抽象 2015-10-21 10:58:24
c++的静态类型初始化是原子操作吗?

多线程的操作下会不会有问题啊?



class Singleton{
private:
Singleton(){cout << "singleton init" <<endl;}
public:
virtual ~Singleton(){cout << "singleton destroyed"<<endl;}
static Singleton& GetInstance(){
static Singleton ins;
return ins;
}
};

...全文
334 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
ri_aje 2015-10-21
  • 打赏
  • 举报
回复
现在的 c++ 标准保证多线程初始化 static 变量不会发生 data race,可以视为原子操作。
  • 打赏
  • 举报
回复
引用 2 楼 wwwzys 的回复:
[quote=引用 1 楼 akirya 的回复:] 不是原子操作。
深入的解释一下吧。[/quote] 只有atmoc类型才是原子操作 google 单件 多线程
fefe82 2015-10-21
  • 打赏
  • 举报
回复
C++11 以后是。 The zero-initialization (8.5) of all block-scope variables with static storage duration (3.7.1) or thread storage duration (3.7.2) is performed before any other initialization takes place. Constant initialization (3.6.2) of a block-scope entity with static storage duration, if applicable, is performed before its block is first entered. An implementation is permitted to perform early initialization of other block-scope variables with static or thread storage duration under the same conditions that an implementation is permitted to statically initialize a variable with static or thread storage duration in namespace scope (3.6.2). Otherwise such a variable is initialized the first time control passes through its declaration; such a variable is considered initialized upon the completion of its initialization. If the initialization exits by throwing an exception, the initialization is not complete, so it will be tried again the next time control enters the declaration. If control enters the declaration concurrently while the variable is being initialized, the concurrent execution shall wait for completion of the initialization. If control re-enters the declaration recursively while the variable is being initialized, the behavior is undefined.
深度抽象 2015-10-21
  • 打赏
  • 举报
回复
引用 1 楼 akirya 的回复:
不是原子操作。
深入的解释一下吧。
  • 打赏
  • 举报
回复
不是原子操作。
ID870177103 2015-10-21
  • 打赏
  • 举报
回复
inline VAR _RANDOM_ () {
static CHAR mTable[32] = {3 ,0X9A319039 ,0X32D9C024 ,0X9B663182 ,0X5DA1F342 ,0XDE3B81E0 ,0XDF0A6FB5 ,0XF103BC02 ,0X48F340FB ,0X7449E56B ,0XBEB1DBB0 ,0XAB5C5918 ,0X946554FD ,0X8C2E680F ,0XEB3D799F ,0XB11EE0B7 ,0X2D436B86 ,0XDA672E2A ,0X1588CA88 ,0XE369735D ,0X904F35F7 ,0XD7158FD6 ,0X6FA6F051 ,0X616E6B96 ,0XAC94EFDC ,0X36413F93 ,0XC622C298 ,0XF5A42AB8 ,0X8A88D77B ,0XF5AD9D0E ,0X8999220B ,0X27FB47B9} ;
static INDEX mIndex1 = 4 ;
static INDEX mIndex2 = 1 ;
static const auto _initializer_ = [] (CHAR (&table)[32] ,INDEX &index1 ,INDEX &index2)->BOOL {
#ifdef _INC_TIME
table[0] = CHAR (time (NULL)) ;
for (INDEX i = 1 ; i < 31 ; i++)
table[i] = 1103515145 * table[i - 1] + 12345 ;
index1 = 3 ;
index2 = 0 ;
for (INDEX i = 0 ; i < 310 ; i++)
_RANDOM_ () ;
return TRUE ;
#else
return FALSE ;
#endif
} ;
static const BOOL mInitialized = _initializer_ (mTable ,mIndex1 ,mIndex2) ;
const VAR ret = VAR ((mTable[mIndex1] += mTable[mIndex2]) >> 1) ;
if (mIndex1++ >= 32)
mIndex1 = 1 ;
if (mIndex2++ >= 32)
mIndex2 = 1 ;
return ret ;
}
mInitialized仅被初始化一次

64,682

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧