thread safety

Hi Keith,

Just a quick comment about your singleton code; there's a potential thread safety issue. In your code:


public static function getInstance() {
if (self::$instance === null) {
self::$instance = new Singleton();
}
return self::$instance;
}

It's possible that thread calls :


if (self::$instance === null)

then gets interrupted and thread 2 calls the same code and passes. Then they both execute:


self::$instance = new Singleton();

This could be a big issue depending on what your singleton is constructing...

I would rather recommend that you do:


private static $instance = new Singleton();

This way it's done before any of the methods can be called, and only once as the static instance is created.

Just my little tip from having been tripped up by the singleton.

Regards,
Steph

Reply

The content of this field is kept private and will not be shown publicly.
  • Allowed HTML tags: <a> <em> <strong> <cite> <code> <ul> <ol> <li> <dl> <dt> <dd> <br> <img> <p>
  • Lines and paragraphs break automatically.

More information about formatting options

Syndicate

Syndicate content

This is not the home of dotProject. It is the home of CaseySoftware, LLC. Any dotProject support questions should be referred to their support forums.

Recent comments

Ads

Text Link Ads Directory of Computers/Tech Blogs