<?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[InderlockIncrement und 32 Bit-Grenze]]></title><description><![CDATA[<p>Hallo alle,</p>
<p>Ich studiere gerade die MSDN zum Thema InterlockedIncrement/-decrement und finde da:</p>
<blockquote>
<p>The variable pointed to by the Addend parameter must be aligned on a 32-bit boundary; otherwise, this function will fail on multiprocessor x86 systems....</p>
</blockquote>
<p>Das macht mich stutzig - schließlich sind Multiprozessor-Systeme in naher Zukunft Allerwelts-Computer. Meine Frage an euch: Welche Maßnahmen muss ich ergreifen, um hier sicher zu gehen? Wie kriege ich also eine Variable auf eine &quot;32-bit boundary&quot; aligned, so dass die Interlocked-Funktionen garantiert klappen? Was müsste ich also z.B. bei folgendem Code noch ändern:</p>
<pre><code class="language-cpp">class Count {
public:
    // ....
    bool inc() { return InterlockedIncrement(&amp;var) != 0; }
    bool dec() { return InterlockedDecrement(&amp;var) == 0; }
    // ....
private:
    long var;
};
</code></pre>
<p>(Zu blöde, dass ich's nicht testen kann, aber mein Rechner hat halt nur einen Prozessor <img
      src="https://www.c-plusplus.net/forum/plugins/nodebb-plugin-emoji/emoji/emoji-one/1f61e.png?v=ab1pehoraso"
      class="not-responsive emoji emoji-emoji-one emoji--disappointed_face"
      title=":("
      alt="😞"
    /> )</p>
<p>Danke für euer Interesse,<br />
Stefan.</p>
]]></description><link>https://www.c-plusplus.net/forum/topic/167321/inderlockincrement-und-32-bit-grenze</link><generator>RSS for Node</generator><lastBuildDate>Thu, 16 Jul 2026 15:44:05 GMT</lastBuildDate><atom:link href="https://www.c-plusplus.net/forum/topic/167321.rss" rel="self" type="application/rss+xml"/><pubDate>Sat, 09 Dec 2006 19:35:26 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to InderlockIncrement und 32 Bit-Grenze on Sat, 09 Dec 2006 19:35:26 GMT]]></title><description><![CDATA[<p>Hallo alle,</p>
<p>Ich studiere gerade die MSDN zum Thema InterlockedIncrement/-decrement und finde da:</p>
<blockquote>
<p>The variable pointed to by the Addend parameter must be aligned on a 32-bit boundary; otherwise, this function will fail on multiprocessor x86 systems....</p>
</blockquote>
<p>Das macht mich stutzig - schließlich sind Multiprozessor-Systeme in naher Zukunft Allerwelts-Computer. Meine Frage an euch: Welche Maßnahmen muss ich ergreifen, um hier sicher zu gehen? Wie kriege ich also eine Variable auf eine &quot;32-bit boundary&quot; aligned, so dass die Interlocked-Funktionen garantiert klappen? Was müsste ich also z.B. bei folgendem Code noch ändern:</p>
<pre><code class="language-cpp">class Count {
public:
    // ....
    bool inc() { return InterlockedIncrement(&amp;var) != 0; }
    bool dec() { return InterlockedDecrement(&amp;var) == 0; }
    // ....
private:
    long var;
};
</code></pre>
<p>(Zu blöde, dass ich's nicht testen kann, aber mein Rechner hat halt nur einen Prozessor <img
      src="https://www.c-plusplus.net/forum/plugins/nodebb-plugin-emoji/emoji/emoji-one/1f61e.png?v=ab1pehoraso"
      class="not-responsive emoji emoji-emoji-one emoji--disappointed_face"
      title=":("
      alt="😞"
    /> )</p>
<p>Danke für euer Interesse,<br />
Stefan.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1189235</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1189235</guid><dc:creator><![CDATA[Interlock]]></dc:creator><pubDate>Sat, 09 Dec 2006 19:35:26 GMT</pubDate></item><item><title><![CDATA[Reply to InderlockIncrement und 32 Bit-Grenze on Sat, 09 Dec 2006 19:56:26 GMT]]></title><description><![CDATA[<p>Ein normales Win32-Programm hat DWORD/long/int Variablen immer an einer 32-Bit Boundary ausgerichtet. Das liegt daran, das das &quot;#pragma pack&quot; auf 8 gestellt ist...<br />
(und ja, ich bin mir bewusst, das packing und alignment zwei verschiedene Dinge sind).<br />
Siehe auch:<br />
<a href="http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dv_vstechart/html/vcconwindowsdataalignmentonipfx86x86-64.asp" rel="nofollow">http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dv_vstechart/html/vcconwindowsdataalignmentonipfx86x86-64.asp</a></p>
<p>Das Folgende Beispiel zeigt dies:</p>
<pre><code class="language-cpp">#include &lt;windows.h&gt;
#include &lt;tchar.h&gt;
#pragma pack(show)
class Foo
{
public:
  char a;
  DWORD b;
  char c;
  char d;
  int e;
};

int _tmain()
{
  Foo *f = new Foo();
  _tprintf(_T(&quot;0x%p\n0x%p\n0x%p\n0x%p\n0x%p\n&quot;), &amp;(f-&gt;a), &amp;(f-&gt;b), &amp;(f-&gt;c), &amp;(f-&gt;d), &amp;(f-&gt;e));
}
</code></pre>
<p>Ergebnis</p>
<pre><code>0x00336D70
0x00336D74
0x00336D78
0x00336D79
0x00336D7C
</code></pre>
<p>Also das &quot;b&quot; und &quot;e&quot; sind jeweils an einer 32-Bit Boundary ausgerichtet...<br />
Ganz anders sieht es natürlich aus, wenn Du ein &quot;pragma pack&quot; verwendet hast:</p>
<pre><code class="language-cpp">#pragma pack(2)
</code></pre>
<pre><code>0x00336D70
0x00336D72
0x00336D76
0x00336D77
0x00336D78
</code></pre>
<p>Unabhängig vom Packing kannst Du es dann noch mit __declspec(align(4)) anpassen:</p>
<pre><code class="language-cpp">#include &lt;windows.h&gt;
#include &lt;tchar.h&gt;

#pragma pack(1)
class Foo
{
public:
  char a;
  __declspec(align(4)) DWORD b;
  char c;
  char d;
  __declspec(align(4)) int e;
};

int _tmain()
{
  Foo *f = new Foo();
  _tprintf(_T(&quot;0x%p\n0x%p\n0x%p\n0x%p\n0x%p\n&quot;), &amp;(f-&gt;a), &amp;(f-&gt;b), &amp;(f-&gt;c), &amp;(f-&gt;d), &amp;(f-&gt;e));
}
</code></pre>
<pre><code>0x00336D70
0x00336D74
0x00336D78
0x00336D79
0x00336D7C
</code></pre>
]]></description><link>https://www.c-plusplus.net/forum/post/1189250</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1189250</guid><dc:creator><![CDATA[Jochen Kalmbach]]></dc:creator><pubDate>Sat, 09 Dec 2006 19:56:26 GMT</pubDate></item><item><title><![CDATA[Reply to InderlockIncrement und 32 Bit-Grenze on Sat, 09 Dec 2006 21:56:06 GMT]]></title><description><![CDATA[<p>Danke, Jochen! Das ist genau das, was ich brauche.</p>
<p>Stefan.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1189266</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1189266</guid><dc:creator><![CDATA[Interlock]]></dc:creator><pubDate>Sat, 09 Dec 2006 21:56:06 GMT</pubDate></item><item><title><![CDATA[Reply to InderlockIncrement und 32 Bit-Grenze on Mon, 11 Dec 2006 04:01:29 GMT]]></title><description><![CDATA[<p>Jo, und ein &quot;new DWORD()&quot; ist unter Windows auch immer auf einer passenden Adresse.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1189857</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1189857</guid><dc:creator><![CDATA[hustbaer]]></dc:creator><pubDate>Mon, 11 Dec 2006 04:01:29 GMT</pubDate></item></channel></rss>