<?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[pthread_create mit Funktionszeiger als Argument]]></title><description><![CDATA[<p>Hallo,<br />
ich habe folgende Funktion:</p>
<pre><code>void qfFunc(time_t timestamp)
{
    cout &lt;&lt; timestamp &lt;&lt; endl;
}
</code></pre>
<p>Diese Funktion wird als Zeiger an eine Klasse übergeben (Konstruktor)</p>
<pre><code>meineKlasse::meineKlasse(string tName, string tPath, void (*queryFunction) (time_t))
{

    pFunc = queryFunction; //pFunc ist private
}

meineKlasse c = meineKlasse(&quot;123&quot;, &quot;456&quot;, qfFunc);
</code></pre>
<p>In der Methode runQuery soll dann ein Thread mit der übergebenen Funktion gestartet werden.</p>
<pre><code>void meineKlasse::runQuery(time_t ts)
{
    pthread_create(&amp;t1, NULL, (void (*)(time_t))pFunc, ts);//t1 ist auch private
}

c.runQuery(12345);
</code></pre>
<p>Beim Kompilieren kriege ich aber folgende Fehlermeldung:</p>
<pre><code>error: invalid conversion from 'void (*)(time_t) {aka void (*)(long int)}' to 'void* (*)(void*)' [-fpermissive]
     pthread_create(&amp;t1, NULL, (void (*)(time_t))pFunc, ts);
                                                          ^
</code></pre>
<p>Kann mir jemand sagen wie ich das beheben kann?</p>
<p>Vielen Dank!</p>
]]></description><link>https://www.c-plusplus.net/forum/topic/328614/pthread_create-mit-funktionszeiger-als-argument</link><generator>RSS for Node</generator><lastBuildDate>Tue, 07 Jul 2026 13:07:17 GMT</lastBuildDate><atom:link href="https://www.c-plusplus.net/forum/topic/328614.rss" rel="self" type="application/rss+xml"/><pubDate>Sun, 19 Oct 2014 11:31:30 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to pthread_create mit Funktionszeiger als Argument on Sun, 19 Oct 2014 11:31:30 GMT]]></title><description><![CDATA[<p>Hallo,<br />
ich habe folgende Funktion:</p>
<pre><code>void qfFunc(time_t timestamp)
{
    cout &lt;&lt; timestamp &lt;&lt; endl;
}
</code></pre>
<p>Diese Funktion wird als Zeiger an eine Klasse übergeben (Konstruktor)</p>
<pre><code>meineKlasse::meineKlasse(string tName, string tPath, void (*queryFunction) (time_t))
{

    pFunc = queryFunction; //pFunc ist private
}

meineKlasse c = meineKlasse(&quot;123&quot;, &quot;456&quot;, qfFunc);
</code></pre>
<p>In der Methode runQuery soll dann ein Thread mit der übergebenen Funktion gestartet werden.</p>
<pre><code>void meineKlasse::runQuery(time_t ts)
{
    pthread_create(&amp;t1, NULL, (void (*)(time_t))pFunc, ts);//t1 ist auch private
}

c.runQuery(12345);
</code></pre>
<p>Beim Kompilieren kriege ich aber folgende Fehlermeldung:</p>
<pre><code>error: invalid conversion from 'void (*)(time_t) {aka void (*)(long int)}' to 'void* (*)(void*)' [-fpermissive]
     pthread_create(&amp;t1, NULL, (void (*)(time_t))pFunc, ts);
                                                          ^
</code></pre>
<p>Kann mir jemand sagen wie ich das beheben kann?</p>
<p>Vielen Dank!</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2422853</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2422853</guid><dc:creator><![CDATA[Bobface]]></dc:creator><pubDate>Sun, 19 Oct 2014 11:31:30 GMT</pubDate></item><item><title><![CDATA[Reply to pthread_create mit Funktionszeiger als Argument on Sun, 19 Oct 2014 12:16:21 GMT]]></title><description><![CDATA[<p>Die Funktionssignatur stimmt nicht. Der Parameter der Funktion, die Du <code>pthread_create</code> übergibst, muss als Parameter einen <code>void*</code> haben und einen <code>void*</code> zurück geben. Eine Konvertierung von Deiner Funktion ist nicht möglich.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2422854</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2422854</guid><dc:creator><![CDATA[tntnet]]></dc:creator><pubDate>Sun, 19 Oct 2014 12:16:21 GMT</pubDate></item><item><title><![CDATA[Reply to pthread_create mit Funktionszeiger als Argument on Sun, 19 Oct 2014 12:21:07 GMT]]></title><description><![CDATA[<p>Die Funktion erwartet void* als Argument.<br />
Deine Funktion qfFunc time_t.<br />
Du musst in qfFunc void* nehmen und das casten.</p>
<p>Edit: Kannst du aber auch ganz einfach automatisieren.</p>
<pre><code>template &lt;void(*fnc)(time_t)&gt;
void trampoline(void *arg)
{
	fnc(*static_cast&lt;time_t*&gt;(arg));
}

meineKlasse::meineKlasse(string tName, string tPath, void (*queryFunction) (time_t))
{

    pFunc = &amp;trampoline&lt;queryFunction&gt;; //pFunc ist private
}

void meineKlasse::runQuery(time_t ts)
{
    pthread_create(&amp;t1, NULL, pFunc, &amp;ts);//t1 ist auch private
}
</code></pre>
<p>(ungetestet)</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2422855</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2422855</guid><dc:creator><![CDATA[Nathan]]></dc:creator><pubDate>Sun, 19 Oct 2014 12:21:07 GMT</pubDate></item><item><title><![CDATA[Reply to pthread_create mit Funktionszeiger als Argument on Sun, 19 Oct 2014 12:49:26 GMT]]></title><description><![CDATA[<p>Danke für die Antwort Nathan, aber leider wirft die Zeile</p>
<pre><code>pFunc = &amp;trampoline&lt;queryFunction&gt;;
</code></pre>
<p>die Fehlermeldung</p>
<pre><code>error: 'queryFunction' is not a valid template argument for type 'void (*)(time_t) {aka void (*)(long int)}'
     pFunc       = &amp;trampoline&lt;queryFunction&gt;;
                 ^
</code></pre>
<pre><code>error: it must be the address of a function with external linkage
</code></pre>
<p>aus. Habe noch nie mit Templates gearbeitet, deswegen fällt es mir schwer hier den Fehler zu erkennen..</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2422860</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2422860</guid><dc:creator><![CDATA[Bobface]]></dc:creator><pubDate>Sun, 19 Oct 2014 12:49:26 GMT</pubDate></item><item><title><![CDATA[Reply to pthread_create mit Funktionszeiger als Argument on Sun, 19 Oct 2014 13:07:53 GMT]]></title><description><![CDATA[<p>Natürlich, geht so nicht.<br />
Du musst aus dem Konstruktor auch noch ein Template machen:</p>
<pre><code>template &lt;void (*queryFunction)(time_t)&gt;
meineKlasse(string tName, string tPath)
{
    pFunc = &amp;trampoline&lt;queryFunction&gt;; //pFunc ist private
}
</code></pre>
<p>Ich kann allerdings auch schnell noch etwas schreiben um das zu vermeiden, moment.</p>
<p>Edit: Hier, wesentlich simpler, musst an den anderen Funktionen gegenüber dem Einstiegspost nichts ändern.</p>
<pre><code>struct trampoline_info
{
	void (*fnc)(time_t);
	time_t arg;
};

void trampoline(void *arg)
{
	auto info = *static_cast&lt;trampoline_info*&gt;(arg);
	info.fnc(info.arg);
}

void meineKlasse::runQuery(time_t ts)
{
    trampoline_info info{pFunc, ts};
    pthread_create(&amp;t1, NULL, &amp;trampoline, &amp;info);//t1 ist auch private
}
</code></pre>
]]></description><link>https://www.c-plusplus.net/forum/post/2422862</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2422862</guid><dc:creator><![CDATA[Nathan]]></dc:creator><pubDate>Sun, 19 Oct 2014 13:07:53 GMT</pubDate></item><item><title><![CDATA[Reply to pthread_create mit Funktionszeiger als Argument on Sun, 19 Oct 2014 13:33:52 GMT]]></title><description><![CDATA[<p>Hi,</p>
<p>Nathan schrieb:</p>
<blockquote>
<p>Edit: Hier, wesentlich simpler, musst an den anderen Funktionen gegenüber dem Einstiegspost nichts ändern.</p>
<pre><code>struct trampoline_info
{
	void (*fnc)(time_t);
	time_t arg;
};

void trampoline(void *arg)
{
	auto info = *static_cast&lt;trampoline_info*&gt;(arg);
	info.fnc(info.arg);
}

void meineKlasse::runQuery(time_t ts)
{
    trampoline_info info{pFunc, ts};
    pthread_create(&amp;t1, NULL, &amp;trampoline, &amp;info);//t1 ist auch private
}
</code></pre>
</blockquote>
<p>Aber hier ist doch das Problem das 'info' zerstoert wird nachdem runQuery fertig ist und der zweite Thread noch darauf zugreift.</p>
<p>Solle man wenn dann nicht eher sowas nutzen:</p>
<pre><code>void trampoline(void *arg)
{
    std::unique_ptr&lt;trampoline_info&gt; info( static_cast&lt;trampoline_info*&gt;(arg));
	info-&gt;fnc(info-&gt;arg);
}

void meineKlasse::runQuery(time_t ts)
{
    auto info = new trampoline_info{pFunc, ts};
    pthread_create(&amp;t1, NULL, &amp;trampoline, info);//t1 ist auch private
}
</code></pre>
]]></description><link>https://www.c-plusplus.net/forum/post/2422869</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2422869</guid><dc:creator><![CDATA[stuxn]]></dc:creator><pubDate>Sun, 19 Oct 2014 13:33:52 GMT</pubDate></item><item><title><![CDATA[Reply to pthread_create mit Funktionszeiger als Argument on Sun, 19 Oct 2014 13:37:04 GMT]]></title><description><![CDATA[<p>stuxn schrieb:</p>
<blockquote>
<p>Hi,</p>
<p>Nathan schrieb:</p>
<blockquote>
<p>Edit: Hier, wesentlich simpler, musst an den anderen Funktionen gegenüber dem Einstiegspost nichts ändern.</p>
<pre><code>struct trampoline_info
{
	void (*fnc)(time_t);
	time_t arg;
};

void trampoline(void *arg)
{
	auto info = *static_cast&lt;trampoline_info*&gt;(arg);
	info.fnc(info.arg);
}

void meineKlasse::runQuery(time_t ts)
{
    trampoline_info info{pFunc, ts};
    pthread_create(&amp;t1, NULL, &amp;trampoline, &amp;info);//t1 ist auch private
}
</code></pre>
</blockquote>
<p>Aber hier ist doch das Problem das 'info' zerstoert wird nachdem runQuery fertig ist und der zweite Thread noch darauf zugreift.</p>
</blockquote>
<p>Klar, das ist ja ein Thread.<br />
Dumm von mir.<br />
Entweder deine Lösung, oder trampoline_info in die Klasse packen, wenn die Klasse länger lebt als der Thread.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2422871</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2422871</guid><dc:creator><![CDATA[Nathan]]></dc:creator><pubDate>Sun, 19 Oct 2014 13:37:04 GMT</pubDate></item><item><title><![CDATA[Reply to pthread_create mit Funktionszeiger als Argument on Sun, 19 Oct 2014 20:42:42 GMT]]></title><description><![CDATA[<p>Benutze C++11 std::thread oder boost::thread.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2422929</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2422929</guid><dc:creator><![CDATA[manni66]]></dc:creator><pubDate>Sun, 19 Oct 2014 20:42:42 GMT</pubDate></item></channel></rss>