<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0"><channel><title><![CDATA[Singleton mit double checked locking]]></title><description><![CDATA[<p>hi<br />
Hab mich mit dem Singleton-Muster beschäftigt und möchte gern ein funktionierendes double checked locking. Der Vorteil ist ja, dass ich verzögerte Instantiierung mit Thread-Sicherheit habe (wenns funzt).<br />
Nun hab ich mal diverse Quellen im Internet zu dem Thema gelesen.<br />
Manche bezeichnen das DCL Singleton ja als Antipattern weil es soviele Probleme hervorrufen kann.</p>
<p>Jetzt hab ich hier mal Java-Code für ein DCL Singleton.</p>
<pre><code>public class Singleton {
	private volatile static Singleton instance;
	private Singleton() {}
	public static getInstance() {
		if(instance == null) {
			synchronized(Singleton.class) {
				if(instance == null )
					instance = new Singleton();
			}
		}
		return instance;
	}
}
</code></pre>
<p>Das Besondere ist, dass instance durch das Schlüsselwort volatile gegen Compiler und CPU-reordering geschützt ist.<br />
Und der synchronized-Block die Thread-Sicherheit garantiert.<br />
Angeblich funzt der Code in Java für ein DCL Singleton einwandfrei (bei Wikipedia gibts ähnlichen Code, nur dass instance nicht volatile ist).</p>
<p>Jetzt hab ich mir mal entsprechenden Code für C++ gebastelt.</p>
<pre><code>#include &lt;afxmt.h&gt;

CCriticalSection cs;

class Singleton{
private:
	static Singleton* volatile instance;	//volatile gegen Compiler und CPU-reordering
	Singleton() {}
public:
	static Singleton* getInstance()
	{
		if(instance == NULL)
		{			
			cs.Lock();		//Synchronisierungs-Block (für thread-safety)
				if(instance == 0)
					instance = new Singleton();
			cs.Unlock();			
		}
		return instance;
	}
};

Singleton* volatile Singleton::instance = new Singleton();

void main(){
	Singleton *singleton = Singleton::getInstance();
}
</code></pre>
<p>Bei Einstellungen -&gt; C/C++ -&gt; Code Generation musste ich auf Multithreaded wechseln damit das läuft.<br />
Laut Microsoft garantiert Visual C++ 2005, dass volatile bei Variablen Compiler und CPU-reordering verhindert. Allerdings gibt es keine solche Garantie in früheren Versionen von Visual C++.<br />
Da muss der Code wohl Memory-Barrieren enthalten.</p>
<p>Ich hab also den kritischen Abschnitt für thread-safety und volatile gegen Compiler und CPU-reordering. Dieser Code sollte doch bei Visual C++ 2005 jetzt einwandfrei funktionieren, oder?</p>
]]></description><link>https://www.c-plusplus.net/forum/topic/171082/singleton-mit-double-checked-locking</link><generator>RSS for Node</generator><lastBuildDate>Mon, 27 Apr 2026 13:46:54 GMT</lastBuildDate><atom:link href="https://www.c-plusplus.net/forum/topic/171082.rss" rel="self" type="application/rss+xml"/><pubDate>Mon, 22 Jan 2007 12:48:45 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to Singleton mit double checked locking on Mon, 22 Jan 2007 12:48:45 GMT]]></title><description><![CDATA[<p>hi<br />
Hab mich mit dem Singleton-Muster beschäftigt und möchte gern ein funktionierendes double checked locking. Der Vorteil ist ja, dass ich verzögerte Instantiierung mit Thread-Sicherheit habe (wenns funzt).<br />
Nun hab ich mal diverse Quellen im Internet zu dem Thema gelesen.<br />
Manche bezeichnen das DCL Singleton ja als Antipattern weil es soviele Probleme hervorrufen kann.</p>
<p>Jetzt hab ich hier mal Java-Code für ein DCL Singleton.</p>
<pre><code>public class Singleton {
	private volatile static Singleton instance;
	private Singleton() {}
	public static getInstance() {
		if(instance == null) {
			synchronized(Singleton.class) {
				if(instance == null )
					instance = new Singleton();
			}
		}
		return instance;
	}
}
</code></pre>
<p>Das Besondere ist, dass instance durch das Schlüsselwort volatile gegen Compiler und CPU-reordering geschützt ist.<br />
Und der synchronized-Block die Thread-Sicherheit garantiert.<br />
Angeblich funzt der Code in Java für ein DCL Singleton einwandfrei (bei Wikipedia gibts ähnlichen Code, nur dass instance nicht volatile ist).</p>
<p>Jetzt hab ich mir mal entsprechenden Code für C++ gebastelt.</p>
<pre><code>#include &lt;afxmt.h&gt;

CCriticalSection cs;

class Singleton{
private:
	static Singleton* volatile instance;	//volatile gegen Compiler und CPU-reordering
	Singleton() {}
public:
	static Singleton* getInstance()
	{
		if(instance == NULL)
		{			
			cs.Lock();		//Synchronisierungs-Block (für thread-safety)
				if(instance == 0)
					instance = new Singleton();
			cs.Unlock();			
		}
		return instance;
	}
};

Singleton* volatile Singleton::instance = new Singleton();

void main(){
	Singleton *singleton = Singleton::getInstance();
}
</code></pre>
<p>Bei Einstellungen -&gt; C/C++ -&gt; Code Generation musste ich auf Multithreaded wechseln damit das läuft.<br />
Laut Microsoft garantiert Visual C++ 2005, dass volatile bei Variablen Compiler und CPU-reordering verhindert. Allerdings gibt es keine solche Garantie in früheren Versionen von Visual C++.<br />
Da muss der Code wohl Memory-Barrieren enthalten.</p>
<p>Ich hab also den kritischen Abschnitt für thread-safety und volatile gegen Compiler und CPU-reordering. Dieser Code sollte doch bei Visual C++ 2005 jetzt einwandfrei funktionieren, oder?</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1214422</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1214422</guid><dc:creator><![CDATA[opengl_noob]]></dc:creator><pubDate>Mon, 22 Jan 2007 12:48:45 GMT</pubDate></item><item><title><![CDATA[Reply to Singleton mit double checked locking on Mon, 22 Jan 2007 14:42:13 GMT]]></title><description><![CDATA[<p>Hi,<br />
was Standard-C++ und das DCL-Singleton angeht, solltest du sicherstellen, dass du Scott Meyers' zweiteiligen Artikel gelesen hast:</p>
<p><a href="http://www.aristeia.com/Papers/DDJ_Jul_Aug_2004.pdf" rel="nofollow">http://www.aristeia.com/Papers/DDJ_Jul_Aug_2004.pdf</a><br />
bzw.<br />
<a href="http://www.aristeia.com/Papers/DDJ_Jul_Aug_2004_revised.pdf" rel="nofollow">http://www.aristeia.com/Papers/DDJ_Jul_Aug_2004_revised.pdf</a></p>
<blockquote>
<p>Dieser Code sollte doch bei Visual C++ 2005 jetzt einwandfrei funktionieren, oder?</p>
</blockquote>
<p>Das kann man dir im VC-Forum sicher besser beantworten...</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1214517</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1214517</guid><dc:creator><![CDATA[HumeSikkins]]></dc:creator><pubDate>Mon, 22 Jan 2007 14:42:13 GMT</pubDate></item><item><title><![CDATA[Reply to Singleton mit double checked locking on Mon, 22 Jan 2007 14:42:24 GMT]]></title><description><![CDATA[<p>Dieser Thread wurde von Moderator/in <a href="http://www.c-plusplus.net/forum/profile.php?mode=viewprofile&amp;u=403" rel="nofollow">HumeSikkins</a> aus dem Forum <a href="http://www.c-plusplus.net/forum/viewforum.php?f=15" rel="nofollow">C++</a> in das Forum <a href="http://www.c-plusplus.net/forum/viewforum.php?f=1" rel="nofollow">MFC (Visual C++)</a> verschoben.</p>
<p>Im Zweifelsfall bitte auch folgende Hinweise beachten:<br />
<a href="http://www.c-plusplus.net/forum/viewtopic.php?t=39405" rel="nofollow">C/C++ Forum :: FAQ - Sonstiges :: Wohin mit meiner Frage?</a></p>
<p><em>Dieses Posting wurde automatisch erzeugt.</em></p>
]]></description><link>https://www.c-plusplus.net/forum/post/1214518</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1214518</guid><dc:creator><![CDATA[C++ Forumbot]]></dc:creator><pubDate>Mon, 22 Jan 2007 14:42:24 GMT</pubDate></item><item><title><![CDATA[Reply to Singleton mit double checked locking on Mon, 22 Jan 2007 21:41:30 GMT]]></title><description><![CDATA[<p>Ja, unter Windows geht das, und ich sehe auch keinen Grund warum der VC 2005 dazwischenpfuschen sollte.<br />
Allerdings ist IMHO diese Zeile falsch</p>
<pre><code class="language-cpp">Singleton* volatile Singleton::instance = new Singleton();
</code></pre>
<p>Wenn du das so schreibst kannst du dir gleich das ganze checken und locken sparen <img
      src="https://www.c-plusplus.net/forum/plugins/nodebb-plugin-emoji/emoji/emoji-one/1f609.png?v=ab1pehoraso"
      class="not-responsive emoji emoji-emoji-one emoji--winking_face"
      title=";)"
      alt="😉"
    /></p>
<p>BTW: auf x86 braucht man doch sowieso keine Memory-Barriers, oder irre ich mich da jetzt?</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1214837</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1214837</guid><dc:creator><![CDATA[hustbaer]]></dc:creator><pubDate>Mon, 22 Jan 2007 21:41:30 GMT</pubDate></item><item><title><![CDATA[Reply to Singleton mit double checked locking on Tue, 23 Jan 2007 09:06:42 GMT]]></title><description><![CDATA[<p>Sorry, muss natürlich mit NULL initialisiert werden <img
      src="https://www.c-plusplus.net/forum/plugins/nodebb-plugin-emoji/emoji/emoji-one/1f609.png?v=ab1pehoraso"
      class="not-responsive emoji emoji-emoji-one emoji--winking_face"
      title=";)"
      alt="😉"
    /><br />
Ja ich glaub auf x86 braucht man die nicht.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1215000</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1215000</guid><dc:creator><![CDATA[opengl_noob]]></dc:creator><pubDate>Tue, 23 Jan 2007 09:06:42 GMT</pubDate></item></channel></rss>