Alexander Nasonov's shared items

Saturday, August 27, 2011

throw AlertException::getInstance();

Someone recently recommended me disruptor. It's Java but there is a port to C++ called distruptor-cpp.
The guys use several boost libraries but the code looks like Java. This "pattern" caught by eyes:

159            throw AlertException::getInstance();

They obviously optimize a memory allocation:

47  /** Singleton pattern to avoid memory allocation in an exception */
48  static AlertException * ALERT_EXCEPTION;
49  static boost::signals2::mutex mutex;


Guess why they need a mutex here? To prevent a creation of multiple instances of ALERT_EXCEPTION in AlertException::getInstance().