本人博客地址:http://www./pwq1989/ 今天群里姐夫推荐了个C++的Actor框架 Theron,就看了下源码,注释比代码还多,业界良心。 源码我还没看完,就看到了他的一个叫StringPool的类,里面通过Ref来生成单例(Singleton),看了下 1 StringPool *StringPool::smInstance = 0; 2 Mutex StringPool::smReferenceMutex; 3 uint32_t StringPool::smReferenceCount = 0; 4 5 6 void StringPool::Reference() 7 { 8 Lock lock(smReferenceMutex); 9 10 // Create the singleton instance if this is the first reference. 11 if (smReferenceCount++ == 0) 12 { 13 IAllocator *const allocator(AllocatorManager::GetCache()); 14 void *const memory(allocator->AllocateAligned(sizeof(StringPool), THERON_CACHELINE_ALIGNMENT)); 15 smInstance = new (memory) StringPool(); 16 } 17 } 我们先不讨论这一段代码,先看看下面的: 大家如果看过C++的Double Check Lock不可靠的这篇paper(地址),作者给出的解决方案是这样的: 1 // First check 2 TYPE* tmp = instance_; 3 // Insert the CPU-specific memory barrier instruction 4 // to synchronize the cache lines on multi-processor. 5 asm ("memoryBarrier"); 6 if (tmp == 0) { 7 // Ensure serialization (guard 8 // constructor acquires lock_). 9 Guard<LOCK> guard (lock_); 10 // Double check. 11 tmp = instance_; 12 if (tmp == 0) { 13 tmp = new TYPE; 14 // Insert the CPU-specific memory barrier instruction 15 // to synchronize the cache lines on multi-processor. 16 asm ("memoryBarrier"); 17 instance_ = tmp; 18 } 19 return tmp; 其实这两个Memory Barrier不用全屏障,第一个用读屏障rmb()就好了。第二个需要一个写屏障wmb()。 我们都知道mb这个东西是为了防止CPU级别的指令乱序被发明出来的,(另一个是编译器级别的,和本篇文章没有多大关系,有兴趣大家可以去研究下),实现也是由平台相关的特殊指令(mfence这样的)组成的。 之所以要写成这样,第二个mb()是为了防止在构造函数完成之前提前对目标赋值,但ctor还没完成,就被挂起,然后第二个线程访问的时候,认为已经构造完毕,进而使用不完整的数据引发奇怪的错误。 (第一个rmb()的作用我觉得是可有可无,加上可能是为了效率把(猜),强制刷新读取instance_的值,防止进入第一个check去竞争那个锁,不加也是不会有错的,因为POSIX规定mutex之间必须保持内存的可见性,所以是不需要担心读到脏数据) <-- 这段是个人意见,欢迎修正。 下面就是我趴了半下午才想明白的问题。。。为啥Theron中那段代码(第一段代码)不需要在lock中添加mb(),后来往下翻了下,发现StringPool的构造函数是空的。。根本就没有内存的写入,当然就不需要wmb()了。 可见,C++的多线程编程,好难 |
|