本文介绍了如何在标准C ++中创建单例设计模式线程安全?当双重检查机制没有完全安全时?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在C ++ 98中是完全线程安全的,如果没有,我们如何在C ++ 98中实现线程安全的单例?



is it completely thread safe in C++ 98 if not, how we can achieve thread safe singleton in C++98 ?

MySingleton * GetInstance()
{
      if (m_pOnlyOneInstance == NULL)
      {
            EnterCriticalSection();
            if (m_pOnlyOneInstance == NULL)
            // Solution 1 and 2 gaps addressed by moving
            // critical section block and by re-doing this check!
            {
                  m_pOnlyOneInstance = new MySingleton();
            }
            LeaveCriticalSection();
      }
      return m_pOnlyOneInstance;
}





我的尝试:



在许多帖子中写道它不是完全线程安全的,在C ++ 11中,call_once()解决了单例thread_safe问题。如何在C ++ 98中完成?



What I have tried:

In many of post it is written that it is not completely thread safe,in C++11 call_once() has solved the singleton thread_safe problem.How to do it in C++98?

推荐答案


这篇关于如何在标准C ++中创建单例设计模式线程安全?当双重检查机制没有完全安全时?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-05 08:35