Thread-safe static variables without mutexing?

前端 未结 5 1171
无人及你
无人及你 2021-02-19 10:32

I remember reading that static variables declared inside methods is not thread-safe. (See What about the Meyer\'s singleton? as mentioned by Todd Gardner)

Dog* M         


        
5条回答
  •  南旧
    南旧 (楼主)
    2021-02-19 11:04

    You are correct that static initialization like that isn't thread safe (here is an article discussing what the compiler will turn it into)

    At the moment, there's no standard, thread safe, portable way to initialize static singletons. Double checked locking can be used, but you need potentially non-portable threading libraries (see a discussion here).

    Here's a few options if thread safety is a must:

    1. Don't be Lazy (loaded): Initialize during static initialization. It could be a problem if another static calls this function in it's constructor, since the order of static initialization is undefined(see here).
    2. Use boost (as you said) or Loki
    3. Roll your own singleton on your supported platforms (should probably be avoided unless you are a threading expert)
    4. Lock a mutex everytime you need access. This could be very slow.

    Example for 1:

    // in a cpp:
    namespace {
        Dog dog("Lassie");
    }
    
    Dog* MyClass::BadMethod()
    {
      return &dog;
    }
    

    Example for 4:

    Dog* MyClass::BadMethod()
    {
      static scoped_ptr pdog;
      {
         Lock l(Mutex);
         if(!pdog.get())
           pdog.reset(new Dog("Lassie"));
      }
      return pdog.get();
    }
    

提交回复
热议问题