<?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[Welchen Include für Critical Section]]></title><description><![CDATA[<p>Hallo, ich bin Anfänger was c++ angeht.</p>
<p>Ich wollte mal Multithreadding ausprobieren und habe mir folgenden code zusammengewurschtelt:</p>
<pre><code class="language-cpp">#include &lt;windows.h&gt;
#include &lt;stdio.h&gt;

#define MAX_THREADS 3 

class Point 
{ 

private: 

   int m_x; 
   int m_y; 

public: 
   void setX(int x) 
   { 

      m_x = x; 

   } 

   void setY(int y) 
   { 

      m_y = y; 

   } 

   void gibaus() {

	printf(&quot;%d &quot;  ,(int)m_x  );	
	printf(&quot;%d \n&quot;,(int)m_y  );

   }

};
Point p;

DWORD WINAPI ThreadFunc(LPVOID data)
{

	for (int index=0; index&lt;50; index++) {

	p.setX((int) data);
	p.setY((int) data);
	p.gibaus();

	} 

	return((DWORD)data);

} 

void main(void)
{

	HANDLE hThread[MAX_THREADS];		
	DWORD  dwThreadID[MAX_THREADS];		

	printf(&quot;\nAlle Threads starten...\n&quot;);

	for (int index=0; index&lt;MAX_THREADS; index++) {

		hThread[index] = CreateThread(NULL,						
												0,							
			   									ThreadFunc,				
												(LPVOID)index,			
												0,							
												&amp;dwThreadID[index]);	
	} 

	for (index=0; index&lt;25; index++) {

	p.setX(4);
	p.setY(4);
	p.gibaus();

	} 

	WaitForMultipleObjects(	MAX_THREADS,
									hThread,			
									TRUE,			
									INFINITE);		

	Sleep(4000);

	for (index=0; index&lt;MAX_THREADS; index++)
		CloseHandle(hThread[index]);

	printf(&quot;\nAlle Threads beendet.\n&quot;);

}
</code></pre>
<p>Jetzt wollte ich versuchen den Zugriff auf &quot;Point&quot; mit Critical Sections Sicher zu machen.</p>
<p>Leider weiß ich nicht wie. Genauer, was ich includiern muß, welche Lib ich einbinden muß und wie genau die benötigten Befehle ich nehmen muß.</p>
<p>Ich habe auch schon Google benutzt, finde aber immer nur so komische Beispiele in denen Klassen für die Threads benutzt werden, die ich gar nicht habe.</p>
<p>PS. Ich benutze den Visual C++ 6.0</p>
]]></description><link>https://www.c-plusplus.net/forum/topic/139662/welchen-include-für-critical-section</link><generator>RSS for Node</generator><lastBuildDate>Mon, 13 Jul 2026 15:55:39 GMT</lastBuildDate><atom:link href="https://www.c-plusplus.net/forum/topic/139662.rss" rel="self" type="application/rss+xml"/><pubDate>Wed, 08 Mar 2006 12:39:46 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to Welchen Include für Critical Section on Wed, 08 Mar 2006 12:39:46 GMT]]></title><description><![CDATA[<p>Hallo, ich bin Anfänger was c++ angeht.</p>
<p>Ich wollte mal Multithreadding ausprobieren und habe mir folgenden code zusammengewurschtelt:</p>
<pre><code class="language-cpp">#include &lt;windows.h&gt;
#include &lt;stdio.h&gt;

#define MAX_THREADS 3 

class Point 
{ 

private: 

   int m_x; 
   int m_y; 

public: 
   void setX(int x) 
   { 

      m_x = x; 

   } 

   void setY(int y) 
   { 

      m_y = y; 

   } 

   void gibaus() {

	printf(&quot;%d &quot;  ,(int)m_x  );	
	printf(&quot;%d \n&quot;,(int)m_y  );

   }

};
Point p;

DWORD WINAPI ThreadFunc(LPVOID data)
{

	for (int index=0; index&lt;50; index++) {

	p.setX((int) data);
	p.setY((int) data);
	p.gibaus();

	} 

	return((DWORD)data);

} 

void main(void)
{

	HANDLE hThread[MAX_THREADS];		
	DWORD  dwThreadID[MAX_THREADS];		

	printf(&quot;\nAlle Threads starten...\n&quot;);

	for (int index=0; index&lt;MAX_THREADS; index++) {

		hThread[index] = CreateThread(NULL,						
												0,							
			   									ThreadFunc,				
												(LPVOID)index,			
												0,							
												&amp;dwThreadID[index]);	
	} 

	for (index=0; index&lt;25; index++) {

	p.setX(4);
	p.setY(4);
	p.gibaus();

	} 

	WaitForMultipleObjects(	MAX_THREADS,
									hThread,			
									TRUE,			
									INFINITE);		

	Sleep(4000);

	for (index=0; index&lt;MAX_THREADS; index++)
		CloseHandle(hThread[index]);

	printf(&quot;\nAlle Threads beendet.\n&quot;);

}
</code></pre>
<p>Jetzt wollte ich versuchen den Zugriff auf &quot;Point&quot; mit Critical Sections Sicher zu machen.</p>
<p>Leider weiß ich nicht wie. Genauer, was ich includiern muß, welche Lib ich einbinden muß und wie genau die benötigten Befehle ich nehmen muß.</p>
<p>Ich habe auch schon Google benutzt, finde aber immer nur so komische Beispiele in denen Klassen für die Threads benutzt werden, die ich gar nicht habe.</p>
<p>PS. Ich benutze den Visual C++ 6.0</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1011501</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1011501</guid><dc:creator><![CDATA[MisterX]]></dc:creator><pubDate>Wed, 08 Mar 2006 12:39:46 GMT</pubDate></item><item><title><![CDATA[Reply to Welchen Include für Critical Section on Wed, 08 Mar 2006 12:56:07 GMT]]></title><description><![CDATA[<pre><code class="language-cpp">#include &lt;windows.h&gt;
</code></pre>
<p>Und die Funktionen heissen<br />
- InitializeCriticalSection<br />
- EnterCriticalSection<br />
- LeaveCriticalSection<br />
- DeleteCriticalSection</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1011516</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1011516</guid><dc:creator><![CDATA[Jochen Kalmbach]]></dc:creator><pubDate>Wed, 08 Mar 2006 12:56:07 GMT</pubDate></item><item><title><![CDATA[Reply to Welchen Include für Critical Section on Wed, 08 Mar 2006 13:02:46 GMT]]></title><description><![CDATA[<p>Danke, aber es ist doch nicht soo einfach, wie ich dachte.<br />
Könnte BITTE Jemand dies in mein kleines Testprogramm einbauen?</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1011528</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1011528</guid><dc:creator><![CDATA[MisterX]]></dc:creator><pubDate>Wed, 08 Mar 2006 13:02:46 GMT</pubDate></item><item><title><![CDATA[Reply to Welchen Include für Critical Section on Wed, 08 Mar 2006 13:07:29 GMT]]></title><description><![CDATA[<p>Also , was mich irgendwie irrietiert ist, dass man ein Pointer auf ein &quot;Critical Section Object&quot; angeben soll. Aber ich müßte ja gar nicht das ganze Objekt &quot;Point&quot; absichern sondern nur die Beiden Zeilen in</p>
<pre><code>void setXY(int x,int y) 
   { 
           //Hier sichern beginnen
            m_x = x;
            m_y = y;
           //und hier beenden

   }
</code></pre>
<p>Wenn ich jetzt diese Funktion verwende</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1011533</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1011533</guid><dc:creator><![CDATA[MisterX]]></dc:creator><pubDate>Wed, 08 Mar 2006 13:07:29 GMT</pubDate></item><item><title><![CDATA[Reply to Welchen Include für Critical Section on Wed, 08 Mar 2006 13:09:46 GMT]]></title><description><![CDATA[<p>MisterX schrieb:</p>
<blockquote>
<p>Könnte BITTE Jemand dies in mein kleines Testprogramm einbauen?</p>
</blockquote>
<p>in der main, bevor die threads gestartet werden: InitializeCriticalSection<br />
in der threadproc. vor die schleife: EnterCriticalSection<br />
...und hinter der schleife: LeaveCriticalSection</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1011534</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1011534</guid><dc:creator><![CDATA[net 0]]></dc:creator><pubDate>Wed, 08 Mar 2006 13:09:46 GMT</pubDate></item><item><title><![CDATA[Reply to Welchen Include für Critical Section on Wed, 08 Mar 2006 13:20:29 GMT]]></title><description><![CDATA[<p>OK, ich habe es versucht, doch ich komme mit dem<br />
&quot;LPCRITICAL_SECTION lpCriticalSection&quot; nicht zurecht.<br />
(Sogar mal was in der MSDN gefunden <img
      src="https://www.c-plusplus.net/forum/plugins/nodebb-plugin-emoji/emoji/emoji-one/1f642.png?v=ab1pehoraso"
      class="not-responsive emoji emoji-emoji-one emoji--slightly_smiling_face"
      title=":)"
      alt="🙂"
    /> )</p>
<p>Kann mir das nicht doch Jemand mal einbauen, sonnst muß ich wieder 1000 Fragen zu diesem neuen Problem stellen, denn an den Beispiel lernt man doch am schnellsten.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1011550</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1011550</guid><dc:creator><![CDATA[MisterX]]></dc:creator><pubDate>Wed, 08 Mar 2006 13:20:29 GMT</pubDate></item><item><title><![CDATA[Reply to Welchen Include für Critical Section on Wed, 08 Mar 2006 13:26:27 GMT]]></title><description><![CDATA[<p>Das heisst doch nicht anderes, als dass du eine Adresse übergeben sollst. <img
      src="https://www.c-plusplus.net/forum/plugins/nodebb-plugin-emoji/emoji/emoji-one/1f644.png?v=ab1pehoraso"
      class="not-responsive emoji emoji-emoji-one emoji--face_with_rolling_eyes"
      title=":rolling_eyes:"
      alt="🙄"
    /></p>
<pre><code class="language-cpp">CRITICAL_SECTION CriSect;

main (..)
{
    ....
    InitializeCriticalSection (&amp;CriSect);
    ....
}
</code></pre>
<p>Usw.</p>
<p>[Edit]Rechtschreibung[/Edit]</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1011556</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1011556</guid><dc:creator><![CDATA[Maffe001]]></dc:creator><pubDate>Wed, 08 Mar 2006 13:26:27 GMT</pubDate></item><item><title><![CDATA[Reply to Welchen Include für Critical Section on Wed, 08 Mar 2006 13:27:04 GMT]]></title><description><![CDATA[<p>MisterX schrieb:</p>
<blockquote>
<p>OK, ich habe es versucht, doch ich komme mit dem<br />
&quot;LPCRITICAL_SECTION lpCriticalSection&quot; nicht zurecht.<br />
(Sogar mal was in der MSDN gefunden <img
      src="https://www.c-plusplus.net/forum/plugins/nodebb-plugin-emoji/emoji/emoji-one/1f642.png?v=ab1pehoraso"
      class="not-responsive emoji emoji-emoji-one emoji--slightly_smiling_face"
      title=":)"
      alt="🙂"
    /> )</p>
</blockquote>
<p>Dann lies doch einfach ein paar Zeilen weiter - &quot;LPCRITICAL_SECTION&quot; ist ein Pointer auf eine CRITICAL_SECTION, d.h. du mußt dort die Adresse (Stichwort &amp;) eines CRITICAL_SECTION-Objekt übergeben, das du vorher definiert hast.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1011562</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1011562</guid><dc:creator><![CDATA[CStoll]]></dc:creator><pubDate>Wed, 08 Mar 2006 13:27:04 GMT</pubDate></item><item><title><![CDATA[Reply to Welchen Include für Critical Section on Wed, 08 Mar 2006 13:47:25 GMT]]></title><description><![CDATA[<p>JUHU, es klappt jetzt, DANKE.</p>
<p>Aber ich habe auch</p>
<pre><code class="language-cpp">Parameters
lpCriticalSection 
[out] Pointer to the critical section object.
</code></pre>
<p>gelesen.</p>
<p>Aber wie kommt man darauf, dass das Objekt</p>
<blockquote>
<p>CRITICAL_SECTION</p>
</blockquote>
<p>mit Bindestrich heißen soll, wenn in der MSDN</p>
<pre><code>[out] Pointer to the &quot;critical section&quot; object.
</code></pre>
<p>steht? <img
      src="https://www.c-plusplus.net/forum/plugins/nodebb-plugin-emoji/emoji/emoji-one/1f615.png?v=ab1pehoraso"
      class="not-responsive emoji emoji-emoji-one emoji--confused_face"
      title=":confused:"
      alt="😕"
    /></p>
<p>Mir ist schon klar, das es keine Leerzeichen enthalten darf, aber ich habe Vieles Probiert: Kleingeschrieben mit Bindestrich, groß mit Unterstrich, nur leider die richtige Version nicht.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1011589</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1011589</guid><dc:creator><![CDATA[MisterX]]></dc:creator><pubDate>Wed, 08 Mar 2006 13:47:25 GMT</pubDate></item><item><title><![CDATA[Reply to Welchen Include für Critical Section on Wed, 08 Mar 2006 13:49:42 GMT]]></title><description><![CDATA[<p>MisterX schrieb:</p>
<blockquote>
<p>Aber wie kommt man darauf, dass das Objekt</p>
<blockquote>
<p>CRITICAL_SECTION</p>
</blockquote>
<p>mit Bindestrich heißen soll, wenn in der MSDN</p>
<pre><code>[out] Pointer to the &quot;critical section&quot; object.
</code></pre>
<p>steht? <img
      src="https://www.c-plusplus.net/forum/plugins/nodebb-plugin-emoji/emoji/emoji-one/1f615.png?v=ab1pehoraso"
      class="not-responsive emoji emoji-emoji-one emoji--confused_face"
      title=":confused:"
      alt="😕"
    /></p>
</blockquote>
<p>Indem man einfach ein wenig weiter liest:</p>
<p>MSDN Library schrieb:</p>
<blockquote>
<p>The process is responsible for allocating the memory used by a critical section object, which it can do by declaring a variable of type <strong>CRITICAL_SECTION</strong></p>
</blockquote>
]]></description><link>https://www.c-plusplus.net/forum/post/1011595</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1011595</guid><dc:creator><![CDATA[MFK]]></dc:creator><pubDate>Wed, 08 Mar 2006 13:49:42 GMT</pubDate></item><item><title><![CDATA[Reply to Welchen Include für Critical Section on Wed, 08 Mar 2006 13:59:57 GMT]]></title><description><![CDATA[<p>DANKE!!!!!!!,</p>
<p>jetzt habe ich erst mal verstanden, wie so ein MSDN Artikel generell aufgebaut ist <img
      src="https://www.c-plusplus.net/forum/plugins/nodebb-plugin-emoji/emoji/emoji-one/1f603.png?v=ab1pehoraso"
      class="not-responsive emoji emoji-emoji-one emoji--grinning_face_with_big_eyes"
      title=":D"
      alt="😃"
    /> <img
      src="https://www.c-plusplus.net/forum/plugins/nodebb-plugin-emoji/emoji/emoji-one/1f603.png?v=ab1pehoraso"
      class="not-responsive emoji emoji-emoji-one emoji--grinning_face_with_big_eyes"
      title=":D"
      alt="😃"
    /></p>
<p>Noch eine Frage zum Thema:<br />
Kann ich die Critical Section auch in mein Objekt selbst einbauen etwa so:</p>
<pre><code class="language-cpp">class Point 
{ 

private: 

	CRITICAL_SECTION CriSect;
    int m_x; 
    int m_y; 

public: 

	Point () {

	InitializeCriticalSection(&amp;CriSect); 
	}

   void setXY(int x,int y) 
   { 
      EnterCriticalSection(&amp;CriSect);
			m_x = x; 
			m_y = y;
      LeaveCriticalSection(&amp;CriSect) ;

   } 

   void gibaus() {
	    EnterCriticalSection(&amp;CriSect);
			printf(&quot;%d &quot;  ,(int)m_x  );	
			printf(&quot;%d \n&quot;,(int)m_y  );
		LeaveCriticalSection(&amp;CriSect) ;
   }

};
</code></pre>
<p>oder ist das falsch?</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1011603</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1011603</guid><dc:creator><![CDATA[MisterX]]></dc:creator><pubDate>Wed, 08 Mar 2006 13:59:57 GMT</pubDate></item><item><title><![CDATA[Reply to Welchen Include für Critical Section on Wed, 08 Mar 2006 14:03:11 GMT]]></title><description><![CDATA[<p>Fast richtig - jetzt brauchst du nur einen Destruktor, der DeleteCriticalSection() aufruft (und entsprechend der &quot;Rule of Three&quot; einen Copy-Ctor und Zuweisungsoperator, sonst steht dir ein Systemcrash bevor).</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1011611</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1011611</guid><dc:creator><![CDATA[CStoll]]></dc:creator><pubDate>Wed, 08 Mar 2006 14:03:11 GMT</pubDate></item><item><title><![CDATA[Reply to Welchen Include für Critical Section on Wed, 08 Mar 2006 14:06:39 GMT]]></title><description><![CDATA[<p>Sowas würde man aber in der Realität nie programmieren. Man verschiebt die Thread-Synchronisation weiter nach aussen.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1011614</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1011614</guid><dc:creator><![CDATA[ee]]></dc:creator><pubDate>Wed, 08 Mar 2006 14:06:39 GMT</pubDate></item><item><title><![CDATA[Reply to Welchen Include für Critical Section on Wed, 08 Mar 2006 14:43:08 GMT]]></title><description><![CDATA[<p>jetzt habe ich den Konstruktor eingebaut</p>
<pre><code class="language-cpp">class Point 
{ 

private: 

    CRITICAL_SECTION CriSect; 
    int m_x; 
    int m_y; 

public: 

    Point () { 
      InitializeCriticalSection(&amp;CriSect); 
    } 

	~Point () {
		DeleteCriticalSection(&amp;CriSect);
	}

   void setXY(int x,int y) 
   { 
      EnterCriticalSection(&amp;CriSect); 
            m_x = x; 
            m_y = y; 
      LeaveCriticalSection(&amp;CriSect) ; 

   } 

   void gibaus() { 
        EnterCriticalSection(&amp;CriSect); 
            printf(&quot;%d &quot;  ,(int)m_x  );    
            printf(&quot;%d \n&quot;,(int)m_y  ); 
        LeaveCriticalSection(&amp;CriSect) ; 
   } 

};
</code></pre>
<p>Aber was ist mit &quot;Rule of Three&quot; gemeint?<br />
Wenn ich danach suche erhalte ich immer Theroien zu 3 großen marktbeherschenden Firmen <img
      src="https://www.c-plusplus.net/forum/plugins/nodebb-plugin-emoji/emoji/emoji-one/1f615.png?v=ab1pehoraso"
      class="not-responsive emoji emoji-emoji-one emoji--confused_face"
      title=":confused:"
      alt="😕"
    /></p>
<p>Und was ist ein einen Copy Construktor?</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1011649</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1011649</guid><dc:creator><![CDATA[MisterX]]></dc:creator><pubDate>Wed, 08 Mar 2006 14:43:08 GMT</pubDate></item><item><title><![CDATA[Reply to Welchen Include für Critical Section on Wed, 08 Mar 2006 14:59:18 GMT]]></title><description><![CDATA[<p>CStoll schrieb:</p>
<blockquote>
<p>sonst steht dir ein Systemcrash bevor</p>
</blockquote>
<p>ach quatsch. windows verträgt eine ganze menge <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>
]]></description><link>https://www.c-plusplus.net/forum/post/1011667</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1011667</guid><dc:creator><![CDATA[net 0]]></dc:creator><pubDate>Wed, 08 Mar 2006 14:59:18 GMT</pubDate></item><item><title><![CDATA[Reply to Welchen Include für Critical Section on Thu, 09 Mar 2006 07:45:37 GMT]]></title><description><![CDATA[<p>MisterX schrieb:</p>
<blockquote>
<p>Aber was ist mit &quot;Rule of Three&quot; gemeint?<br />
Wenn ich danach suche erhalte ich immer Theroien zu 3 großen marktbeherschenden Firmen <img
      src="https://www.c-plusplus.net/forum/plugins/nodebb-plugin-emoji/emoji/emoji-one/1f615.png?v=ab1pehoraso"
      class="not-responsive emoji emoji-emoji-one emoji--confused_face"
      title=":confused:"
      alt="😕"
    /></p>
</blockquote>
<p>Kurzform: Wenn eine Klasse eine der &quot;großen 3&quot; Methoden (Destruktor, Copy-Construktor, operator=) benötigt, benötigt sie typischerweise alle drei.</p>
<p>oder ausführlicher für deinen Fall:<br />
Dein Dtor zerlegt die Critical Section - die vom Compiler erzeugten Kopier-Methoden kopieren diese jedoch blind weiter. D.h. wenn ein Point-Objekt kopiert wird, arbeiten beide Objekte mit der selben Critical Section (das führt im besten Fall dazu, daß sie sich gegenseitig aussperren, im schlimmsten Fall versuchst du irgendwann, eine nicht-initialisierte CritSec zu verwenden.</p>
<p><a class="plugin-mentions-user plugin-mentions-a" href="https://www.c-plusplus.net/forum/uid/3162">@net</a>: Wenn Windows das kompensieren kann, hast du viel Glück gehabt - ich würde mich nicht darauf verlassen <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>
]]></description><link>https://www.c-plusplus.net/forum/post/1012185</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1012185</guid><dc:creator><![CDATA[CStoll]]></dc:creator><pubDate>Thu, 09 Mar 2006 07:45:37 GMT</pubDate></item></channel></rss>