Changeset 3567


Ignore:
Timestamp:
Jan 8, 2017, 11:39:52 AM (5 years ago)
Author:
Peter
Message:

refs #876

Implementing The Double-Checked Locking Pattern but without the
atomicity to be 100% thread-safe.

Location:
branches/0.14-stable/yat/random
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • branches/0.14-stable/yat/random/random.cc

    r3469 r3567  
    3939
    4040  RNG* RNG::instance_=NULL;
     41  boost::mutex RNG::init_mutex_;
    4142
    4243  RNG::RNG(void)
     
    5960  RNG* RNG::instance(void)
    6061  {
    61     if (instance_==NULL)
    62       instance_ = new RNG;
     62    if (instance_==NULL) {
     63      boost::unique_lock<boost::mutex> lock(init_mutex_);
     64      if (instance_==NULL)
     65        instance_ = new RNG;
     66    }
    6367    return instance_;
    6468  }
  • branches/0.14-stable/yat/random/random.h

    r3518 r3567  
    200200    // guard needs to be mutable because major mission for it is to protect seed_ against multi-access, and seed_ is mutable...
    201201    mutable boost::mutex mutex_;
     202    static boost::mutex init_mutex_;
    202203  };
    203204
Note: See TracChangeset for help on using the changeset viewer.