[Development] QFutureInterface

Bauer, Christian christianbauer at siemens.com
Fri Dec 11 11:05:00 CET 2015


Hi,

Sorry for the delay.

> Yes, please provide a short example with std::promise, which, afaiu, works for your use-case.
Below is a simplified example where a client calls an API function that blocks until the result is actually available.
The request is sent to a queue, where a "server" thread sends a network packet to another host.
As soon as the reply arrives from the network, another "server" thread uses the promise for delivering the result to the client.


Thread 1 (called by client)
--------
std::promise<int> prom;
// put promise into map for later retrieval by server thread
promiseMap.insert(id, prom);
// trigger work
queue.enqueueWork(id, workItem);
// get future and block until result becomes available
std::future fut = prom.get_future();
return fut.get();


Thread 2 ("server" request thread)
--------
workItem = queue.dequeueWork();
// do some work, then send the request
sendRequest(workItem.id, workItem.content);


Thread 3 ("server" reply thread)
--------
reply = receiveReply();
// retrieve promise for the original request
std::promise<int> prom = promiseMap.get(reply.id);
// deliver result to original requestor
prom.set_value(reply.value);



HTH
 Christian



More information about the Development mailing list