<?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[_endthreadex beendet alle Threads]]></title><description><![CDATA[<p>Hallo,<br />
ich bringe mir gerade Multi-Threading bei und habe mit folgendem Beispielcode ein kleines Problem:</p>
<pre><code class="language-cpp">int main()
{
	HANDLE	hThrds[2];
	DWORD		aThreadID[2];
	std::stringstream ssText;
	int			iKey;
	char		cAbfrage;
	bool		bQuit,bStatus[2];

	bQuit = bStatus[0] = bStatus[1] = false;
	while (!bQuit)
	{
		if(kbhit())
		{
			cAbfrage = _getch();

			switch (cAbfrage)
			{
				case 'q':
				case 'Q':
				{
					bQuit = true;
					break;
				}

				case 49:
				case 50:
				{
					ssText &lt;&lt; cAbfrage;
					ssText &gt;&gt; iKey;
					iKey--;
					bStatus[iKey] = !bStatus[iKey];
					if (bStatus[iKey])
					{
						if (1 == iKey)
							hThrds[iKey] = (HANDLE) _beginthreadex(0,0,
								(unsigned (_stdcall *)(void *)) ThreadTwo,
								NULL,0,  (unsigned *) &amp;aThreadID[iKey]);
						else
							hThrds[iKey] = (HANDLE) _beginthreadex(0,0,
								(unsigned (_stdcall *)(void *)) ThreadThree,
								NULL,0,  (unsigned *) &amp;aThreadID[iKey]);
					}
					else
					{
						_endthreadex(aThreadID[iKey]);
					}

					break;
				}
				default:
					break;
			}
		}
	}

	for (iKey=0;iKey&lt;2;iKey++)
	{
		if (bStatus[iKey])
		_endthreadex(aThreadID[iKey]);
	  CloseHandle(hThrds[iKey]);
	}
	return(0);
}
</code></pre>
<p>Leider ist es aber so, dass ich, wenn ich die 1 oder 2 drücke (also Zeichen 49 bzw. 50) auch die main-Funktion beendet wird? Woran liegt das?</p>
<p>Vielen Dank für eure Hilfe!<br />
V.K.</p>
]]></description><link>https://www.c-plusplus.net/forum/topic/212929/_endthreadex-beendet-alle-threads</link><generator>RSS for Node</generator><lastBuildDate>Wed, 14 Oct 2026 15:30:36 GMT</lastBuildDate><atom:link href="https://www.c-plusplus.net/forum/topic/212929.rss" rel="self" type="application/rss+xml"/><pubDate>Mon, 12 May 2008 13:26:20 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to _endthreadex beendet alle Threads on Mon, 12 May 2008 14:00:38 GMT]]></title><description><![CDATA[<p>Hallo,<br />
ich bringe mir gerade Multi-Threading bei und habe mit folgendem Beispielcode ein kleines Problem:</p>
<pre><code class="language-cpp">int main()
{
	HANDLE	hThrds[2];
	DWORD		aThreadID[2];
	std::stringstream ssText;
	int			iKey;
	char		cAbfrage;
	bool		bQuit,bStatus[2];

	bQuit = bStatus[0] = bStatus[1] = false;
	while (!bQuit)
	{
		if(kbhit())
		{
			cAbfrage = _getch();

			switch (cAbfrage)
			{
				case 'q':
				case 'Q':
				{
					bQuit = true;
					break;
				}

				case 49:
				case 50:
				{
					ssText &lt;&lt; cAbfrage;
					ssText &gt;&gt; iKey;
					iKey--;
					bStatus[iKey] = !bStatus[iKey];
					if (bStatus[iKey])
					{
						if (1 == iKey)
							hThrds[iKey] = (HANDLE) _beginthreadex(0,0,
								(unsigned (_stdcall *)(void *)) ThreadTwo,
								NULL,0,  (unsigned *) &amp;aThreadID[iKey]);
						else
							hThrds[iKey] = (HANDLE) _beginthreadex(0,0,
								(unsigned (_stdcall *)(void *)) ThreadThree,
								NULL,0,  (unsigned *) &amp;aThreadID[iKey]);
					}
					else
					{
						_endthreadex(aThreadID[iKey]);
					}

					break;
				}
				default:
					break;
			}
		}
	}

	for (iKey=0;iKey&lt;2;iKey++)
	{
		if (bStatus[iKey])
		_endthreadex(aThreadID[iKey]);
	  CloseHandle(hThrds[iKey]);
	}
	return(0);
}
</code></pre>
<p>Leider ist es aber so, dass ich, wenn ich die 1 oder 2 drücke (also Zeichen 49 bzw. 50) auch die main-Funktion beendet wird? Woran liegt das?</p>
<p>Vielen Dank für eure Hilfe!<br />
V.K.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1507481</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1507481</guid><dc:creator><![CDATA[VerbalKint]]></dc:creator><pubDate>Mon, 12 May 2008 14:00:38 GMT</pubDate></item><item><title><![CDATA[Reply to _endthreadex beendet alle Threads on Mon, 12 May 2008 14:56:21 GMT]]></title><description><![CDATA[<p>VerbalKint schrieb:</p>
<blockquote>
<p>Leider ist es aber so, dass ich, wenn ich die 1 oder 2 drücke (also Zeichen 49 bzw. 50) auch die main-Funktion beendet wird? Woran liegt das?</p>
</blockquote>
<p>Daran, dass du _endthreadex benutzt. Diese Funktion tut nicht das, was du glaubst. Sie beendet nicht einen bestimmten Thread, der über den Parameter identifiziert wird, sondern den Thread, aus dem du sie aufrufst.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1507529</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1507529</guid><dc:creator><![CDATA[MFK]]></dc:creator><pubDate>Mon, 12 May 2008 14:56:21 GMT</pubDate></item><item><title><![CDATA[Reply to _endthreadex beendet alle Threads on Mon, 12 May 2008 15:35:26 GMT]]></title><description><![CDATA[<p>Okay, und wie kann ich dann einen bestimmten Thread beenden, den ich in der main-Funktion aufrufe?<br />
Und wie könnte ich in der while-Schleife der main-Funktion abfragen, ob ein Thread noch läuft oder nicht?</p>
<p>Danke schön!<br />
V.K.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1507537</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1507537</guid><dc:creator><![CDATA[VerbalKint]]></dc:creator><pubDate>Mon, 12 May 2008 15:35:26 GMT</pubDate></item><item><title><![CDATA[Reply to _endthreadex beendet alle Threads on Mon, 12 May 2008 15:59:05 GMT]]></title><description><![CDATA[<p>VerbalKint schrieb:</p>
<blockquote>
<p>Okay, und wie kann ich dann einen bestimmten Thread beenden, den ich in der main-Funktion aufrufe?</p>
</blockquote>
<p>Am saubersten, indem du dem Thread signalisierst, dass er sich selbst beenden soll.</p>
<p>VerbalKint schrieb:</p>
<blockquote>
<p>Und wie könnte ich in der while-Schleife der main-Funktion abfragen, ob ein Thread noch läuft oder nicht?</p>
</blockquote>
<p>WaitForSingleObject auf das Threadhandle.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1507561</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1507561</guid><dc:creator><![CDATA[MFK]]></dc:creator><pubDate>Mon, 12 May 2008 15:59:05 GMT</pubDate></item><item><title><![CDATA[Reply to _endthreadex beendet alle Threads on Mon, 12 May 2008 17:18:54 GMT]]></title><description><![CDATA[<p>Danke für die Hilfe.<br />
V.K.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1507612</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1507612</guid><dc:creator><![CDATA[VerbalKint]]></dc:creator><pubDate>Mon, 12 May 2008 17:18:54 GMT</pubDate></item></channel></rss>