<?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[Frage zu Einsatz von std::promise und std::future]]></title><description><![CDATA[<p>Hallo,</p>
<p>ich habe mal eine Frage zur Nutzung des Promise-Future-Mechanismus.<br />
Folgende Ausgangslage: ich nutze eine C-Schnittstelle für Profibus-Kommunikation.<br />
Diese gibt Signale beim Initialisieren und beim Beenden durch asynchrone Aufrufe kund.<br />
Da der restliche Ablauf sonst synchron ist, wollte ich für den init und das deinint nicht extra groß mit Threading, MessageQueue usw. anfangen.<br />
Daher leite ich die Signale mit den futures durch.</p>
<p>Aktuell ist es so, dass es ein globales Promise-Objekt gibt. Ich setze hier immer erst ein neues, dann starte ich den API-Call, dann hole ich das Future und von dem das Ergebnis, welches im Callback gesetzt wird.</p>
<pre><code class="language-cpp">/* start the Device */
g_async_pnio_result = std::promise&lt;void*&gt;();
if (PNIOD_start_sync(g_devHndl) != PNIO_OK)
	return 14;

//wait for async signals
auto param = g_async_pnio_result.get_future().get();
g_async_pnio_result = std::promise&lt;void*&gt;();
PNIOD_connect_async_rsp((PNIOD_CBF_ASYNC_CONNECT_IND_PARAMS_TYPE*)param);

param = g_async_pnio_result.get_future().get();
g_async_pnio_result = std::promise&lt;void*&gt;();
PNIOD_ownership_async_rsp((PNIOD_CBF_ASYNC_OWNERSHIP_IND_PARAMS_TYPE*)param);

param = g_async_pnio_result.get_future().get();
g_async_pnio_result = std::promise&lt;void*&gt;();
prmEndIndFunc((PNIOD_CBF_ASYNC_PRM_END_IND_PARAMS_TYPE*)param);
PNIOD_prm_end_async_rsp((PNIOD_CBF_ASYNC_PRM_END_IND_PARAMS_TYPE*)param);

param = g_async_pnio_result.get_future().get();
PNIOD_indata_async_rsp((PNIOD_CBF_ASYNC_INDATA_IND_PARAMS_TYPE*)param);
</code></pre>
<p>Das ganze wiederhole ich dann einfach. Meine Frage: vom prinzipiellen Ablauf ist das Nutzen der Promise-Future-Objekte so in Ordnung, oder?<br />
Man darf sie ja nur 1x benutzen, daher erzeuge ich immer ein neues Objekt.</p>
<p>Das future hole ich mir mit get_future und greife dann direkt auf das Ergebnis zu. Es kann also sein, dass<br />
<em>set_value</em> im Callback schon gerufen wurde, bevor der <em>get_future</em>-Call kommt.<br />
Ist das in Ordnung, oder muss das future vorher aus dem Promise geholt worden sein?</p>
]]></description><link>https://www.c-plusplus.net/forum/topic/355195/frage-zu-einsatz-von-std-promise-und-std-future</link><generator>RSS for Node</generator><lastBuildDate>Wed, 09 Sep 2026 19:20:37 GMT</lastBuildDate><atom:link href="https://www.c-plusplus.net/forum/topic/355195.rss" rel="self" type="application/rss+xml"/><pubDate>Tue, 28 Jan 2025 13:34:00 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to Frage zu Einsatz von std::promise und std::future on Tue, 28 Jan 2025 13:34:00 GMT]]></title><description><![CDATA[<p>Hallo,</p>
<p>ich habe mal eine Frage zur Nutzung des Promise-Future-Mechanismus.<br />
Folgende Ausgangslage: ich nutze eine C-Schnittstelle für Profibus-Kommunikation.<br />
Diese gibt Signale beim Initialisieren und beim Beenden durch asynchrone Aufrufe kund.<br />
Da der restliche Ablauf sonst synchron ist, wollte ich für den init und das deinint nicht extra groß mit Threading, MessageQueue usw. anfangen.<br />
Daher leite ich die Signale mit den futures durch.</p>
<p>Aktuell ist es so, dass es ein globales Promise-Objekt gibt. Ich setze hier immer erst ein neues, dann starte ich den API-Call, dann hole ich das Future und von dem das Ergebnis, welches im Callback gesetzt wird.</p>
<pre><code class="language-cpp">/* start the Device */
g_async_pnio_result = std::promise&lt;void*&gt;();
if (PNIOD_start_sync(g_devHndl) != PNIO_OK)
	return 14;

//wait for async signals
auto param = g_async_pnio_result.get_future().get();
g_async_pnio_result = std::promise&lt;void*&gt;();
PNIOD_connect_async_rsp((PNIOD_CBF_ASYNC_CONNECT_IND_PARAMS_TYPE*)param);

param = g_async_pnio_result.get_future().get();
g_async_pnio_result = std::promise&lt;void*&gt;();
PNIOD_ownership_async_rsp((PNIOD_CBF_ASYNC_OWNERSHIP_IND_PARAMS_TYPE*)param);

param = g_async_pnio_result.get_future().get();
g_async_pnio_result = std::promise&lt;void*&gt;();
prmEndIndFunc((PNIOD_CBF_ASYNC_PRM_END_IND_PARAMS_TYPE*)param);
PNIOD_prm_end_async_rsp((PNIOD_CBF_ASYNC_PRM_END_IND_PARAMS_TYPE*)param);

param = g_async_pnio_result.get_future().get();
PNIOD_indata_async_rsp((PNIOD_CBF_ASYNC_INDATA_IND_PARAMS_TYPE*)param);
</code></pre>
<p>Das ganze wiederhole ich dann einfach. Meine Frage: vom prinzipiellen Ablauf ist das Nutzen der Promise-Future-Objekte so in Ordnung, oder?<br />
Man darf sie ja nur 1x benutzen, daher erzeuge ich immer ein neues Objekt.</p>
<p>Das future hole ich mir mit get_future und greife dann direkt auf das Ergebnis zu. Es kann also sein, dass<br />
<em>set_value</em> im Callback schon gerufen wurde, bevor der <em>get_future</em>-Call kommt.<br />
Ist das in Ordnung, oder muss das future vorher aus dem Promise geholt worden sein?</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2623438</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2623438</guid><dc:creator><![CDATA[Pellaeon]]></dc:creator><pubDate>Tue, 28 Jan 2025 13:34:00 GMT</pubDate></item></channel></rss>