<?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[Funktionszeiger auf eine Methode einer nicht-statischen Klasse? (für PThread) - gelöst]]></title><description><![CDATA[<p>Hallo liebe Community,</p>
<p>leider komme ich trotz dieses Tutorials (<a href="http://www.newty.de/fpt/index.html" rel="nofollow">http://www.newty.de/fpt/index.html</a>) bis jetzt nicht weiter. Ich bin mir zwar nicht ganz sicher, ob das Thema hierhin oder in den Linuxbereich gehört, aber da es ja eigentlich kein PThreadproblem ist, habe ich es mal in den C++-Bereich gestellt.</p>
<p>Hier ein Minimalbeispiel für mein Problem:</p>
<pre><code class="language-cpp">/* main.cpp */
#include &lt;iostream&gt;
#include &lt;stdio.h&gt;
#include &lt;stdlib.h&gt;
#include &lt;pthread.h&gt;
#include &lt;sched.h&gt;

using namespace std;

class Klasse {
public:
	void* machwasnormales(void* arg) { cout &lt;&lt; &quot;normales hallo&quot; &lt;&lt; endl; };
	static void* machwasstatisches(void* arg) { cout &lt;&lt; &quot;statisches hallo&quot; &lt;&lt; endl; };
};

int main(int argc, char **argv)
{
	pthread_attr_t tattr;
	pthread_t tid;
	int ret;
	pthread_t thread;
	ret = pthread_attr_init (&amp;tattr);
	Klasse instanz;
	int iret = pthread_create(&amp;thread, &amp;tattr, &amp;(Klasse::machwasstatisches), NULL);
	pthread_join(thread, NULL);
	return 0;
}
</code></pre>
<pre><code>g++ -pthread main.cpp
</code></pre>
<p>So, wie ihr euch ja wahrscheinlich schon denken könnt, würde ich ganz gern das &quot;normale hallo&quot; aus nem Thread heraus zu sehen bekommen.</p>
<p>Wär super, wenn ihr mir helfen könntet.<br />
Vielen Dank im Voraus.</p>
<p>Dobias</p>
<p>PS: Falls es irgendwie wichtig ist: Ich arbeite unter Ubuntu 9.04 mit g++ 4.3.3.</p>
]]></description><link>https://www.c-plusplus.net/forum/topic/243180/funktionszeiger-auf-eine-methode-einer-nicht-statischen-klasse-für-pthread-gelöst</link><generator>RSS for Node</generator><lastBuildDate>Sat, 05 Sep 2026 22:46:34 GMT</lastBuildDate><atom:link href="https://www.c-plusplus.net/forum/topic/243180.rss" rel="self" type="application/rss+xml"/><pubDate>Sat, 13 Jun 2009 18:34:36 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to Funktionszeiger auf eine Methode einer nicht-statischen Klasse? (für PThread) - gelöst on Sat, 13 Jun 2009 20:56:24 GMT]]></title><description><![CDATA[<p>Hallo liebe Community,</p>
<p>leider komme ich trotz dieses Tutorials (<a href="http://www.newty.de/fpt/index.html" rel="nofollow">http://www.newty.de/fpt/index.html</a>) bis jetzt nicht weiter. Ich bin mir zwar nicht ganz sicher, ob das Thema hierhin oder in den Linuxbereich gehört, aber da es ja eigentlich kein PThreadproblem ist, habe ich es mal in den C++-Bereich gestellt.</p>
<p>Hier ein Minimalbeispiel für mein Problem:</p>
<pre><code class="language-cpp">/* main.cpp */
#include &lt;iostream&gt;
#include &lt;stdio.h&gt;
#include &lt;stdlib.h&gt;
#include &lt;pthread.h&gt;
#include &lt;sched.h&gt;

using namespace std;

class Klasse {
public:
	void* machwasnormales(void* arg) { cout &lt;&lt; &quot;normales hallo&quot; &lt;&lt; endl; };
	static void* machwasstatisches(void* arg) { cout &lt;&lt; &quot;statisches hallo&quot; &lt;&lt; endl; };
};

int main(int argc, char **argv)
{
	pthread_attr_t tattr;
	pthread_t tid;
	int ret;
	pthread_t thread;
	ret = pthread_attr_init (&amp;tattr);
	Klasse instanz;
	int iret = pthread_create(&amp;thread, &amp;tattr, &amp;(Klasse::machwasstatisches), NULL);
	pthread_join(thread, NULL);
	return 0;
}
</code></pre>
<pre><code>g++ -pthread main.cpp
</code></pre>
<p>So, wie ihr euch ja wahrscheinlich schon denken könnt, würde ich ganz gern das &quot;normale hallo&quot; aus nem Thread heraus zu sehen bekommen.</p>
<p>Wär super, wenn ihr mir helfen könntet.<br />
Vielen Dank im Voraus.</p>
<p>Dobias</p>
<p>PS: Falls es irgendwie wichtig ist: Ich arbeite unter Ubuntu 9.04 mit g++ 4.3.3.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1726105</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1726105</guid><dc:creator><![CDATA[Dobias]]></dc:creator><pubDate>Sat, 13 Jun 2009 20:56:24 GMT</pubDate></item><item><title><![CDATA[Reply to Funktionszeiger auf eine Methode einer nicht-statischen Klasse? (für PThread) - gelöst on Sat, 13 Jun 2009 19:26:58 GMT]]></title><description><![CDATA[<p>Was genau ist jetzt dein Problem?<br />
Wir sind hier nicht bei &quot;Rate mal mit Rosenthal&quot;</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1726123</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1726123</guid><dc:creator><![CDATA[Knochenwerfer]]></dc:creator><pubDate>Sat, 13 Jun 2009 19:26:58 GMT</pubDate></item><item><title><![CDATA[Reply to Funktionszeiger auf eine Methode einer nicht-statischen Klasse? (für PThread) - gelöst on Sat, 13 Jun 2009 19:56:16 GMT]]></title><description><![CDATA[<p>Sorry, wenn ich mich so unverständlich ausgedrückt habe.<br />
Ich möchte einen Thread starten, der nicht mit Klasse::machwasstatisches läuft, was ja wunderbar funktioniert, sondern einen, der instanz::machwasnormales benutzt.<br />
Ein einfaches</p>
<pre><code class="language-cpp">int iret = pthread_create(&amp;thread, &amp;tattr, &amp;(instanz.machwasnormales), NULL);
</code></pre>
<p>tut es hier natürlich nicht.</p>
<pre><code>main.cpp: In function ‘int main(int, char**)’:
main.cpp:24: error: ISO C++ forbids taking the address of a bound member function to form a pointer to member function.  Say ‘&amp;Klasse::machwasnormales’
main.cpp:24: error: cannot convert ‘void* (Klasse::*)(void*)’ to ‘void* (*)(void*)’ for argument ‘3’ to ‘int pthread_create(pthread_t*, const pthread_attr_t*, void* (*)(void*), void*)’
</code></pre>
<p>Schon allein, weil der Funktionsaufruf ja noch den this-Zeiger der Instanz als Parameter mitbekommt.<br />
Meine Frage ist jetzt: Wie macht ich es richtig?</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1726132</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1726132</guid><dc:creator><![CDATA[Dobias]]></dc:creator><pubDate>Sat, 13 Jun 2009 19:56:16 GMT</pubDate></item><item><title><![CDATA[Reply to Funktionszeiger auf eine Methode einer nicht-statischen Klasse? (für PThread) - gelöst on Sat, 13 Jun 2009 20:16:30 GMT]]></title><description><![CDATA[<p>Du musst ganz einfach das Objekt selbst auch noch irgendwie mitliefern.<br />
Sprich, schlussendlich hast du eine statische Funktion und übergibst das Objekt irgendwie anderst. Vlt. wird ja irgendwas von Haus aus unterstützt (wie z.B zusätzliche Parameter) oder ansonsten musst du da einen Umweg machen, um die Auswahl des Objektes anderst treffen.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1726141</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1726141</guid><dc:creator><![CDATA[drakon]]></dc:creator><pubDate>Sat, 13 Jun 2009 20:16:30 GMT</pubDate></item><item><title><![CDATA[Reply to Funktionszeiger auf eine Methode einer nicht-statischen Klasse? (für PThread) - gelöst on Sat, 13 Jun 2009 21:20:50 GMT]]></title><description><![CDATA[<p>Ok, vielen Dank. Hab was passendes gefunden.<br />
<a href="http://www.tuxtips.org/?p=5" rel="nofollow">http://www.tuxtips.org/?p=5</a></p>
<pre><code class="language-cpp">#include &lt;iostream&gt;
#include &lt;string&gt;
#include &lt;pthread.h&gt;

using namespace std;
class example
{
        public:
                string s;
                void say_hi() { cout&lt;&lt;&quot;hello &quot;&lt;&lt;s&lt;&lt;endl; }
                void (example::*func())() { return &amp;example::say_hi; }
                void get_hi()
                {
                        void ((example::*foo)()) =  func();
                        (this-&gt;*foo)();
                }
                static void* thread_maker(void* dat_s);
};

struct val_st
{
        example* e;
        void (example::*func)();
};

void* example::thread_maker(void* dat_s)
{
        struct val_st* v = static_cast&lt;val_st*&gt;(dat_s);
        example* e = v-&gt;e;
        void ((example::*f)()) = v-&gt;func;
        (e-&gt;*f)();
        return NULL;
}

int main()
{
        pthread_t launch_thread;
        example foo;
        example bar;
        val_st v;
        foo.s = &quot;world&quot;;
        bar.s = &quot;there&quot;;
        foo.get_hi();
        bar.get_hi();
        void ((example::*f)()) =  bar.func();
        (foo.*f)();
        v.e = &amp;foo;
        v.func = foo.func();
        pthread_create(&amp;launch_thread, NULL, &amp;example::thread_maker, static_cast&lt;void*&gt;(&amp;v));
        pthread_join(launch_thread,NULL);
        return 0;
}
</code></pre>
]]></description><link>https://www.c-plusplus.net/forum/post/1726155</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1726155</guid><dc:creator><![CDATA[Dobias]]></dc:creator><pubDate>Sat, 13 Jun 2009 21:20:50 GMT</pubDate></item></channel></rss>