25.02.2015 Views

Template Method - Laurent Henocque

Template Method - Laurent Henocque

Template Method - Laurent Henocque

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Double Checked Locking<br />

class Singleton {<br />

public:<br />

static Singleton * instance();<br />

private:<br />

static Singleton * self;<br />

static SEMAPHORE key;<br />

}<br />

Singleton *Singleton::instance() {<br />

if (self == 0) {<br />

if ( lock(key) >= 0 ) {<br />

if (self == 0 ); //double-check!<br />

self = new Singleton;<br />

unlock (key);<br />

} } }

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!