内容纲要

[cpp]
struct Database {
// same members as before, but then…
static Database& instance();
private:
static boost::atomic<Database*> instance;
static boost::mutex mtx;
};

Database& Database::instance()
{
Database* db = instance.load(boost::memory_order_consume);

if (!db) {
boost::mutex::scoped_lock lock(mtx);
db = instance.load(boost::memory_order_consume);

if (!db) {
db = new Database();
instance.store(db, boost::memory_order_release);
}
}
}
[/cpp]

发表评论

电子邮件地址不会被公开。 必填项已用*标注