O'Reilly logo

Mastering C++ Programming by Jeganathan Swaminathan

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Asynchronous message passing using the concurrency support library

Let's slightly modify main.cpp, the Hello World program we wrote in the previous section. Let's understand how we could pass a message from a Thread function to the caller function asynchronously:

#include <iostream>#include <future>using namespace std;void sayHello( promise<string> promise_ ) {  promise_.set_value ( "Hello Concurrency support library!" );}int main ( ) {  promise<string> promiseObj;  future<string> futureObj = promiseObj.get_future( );  async ( launch::async, sayHello, move( promiseObj ) );  cout << futureObj.get( ) << endl;  return 0;}

In the previous program, promiseObj is used by the sayHello() thread function to pass the message to the main thread asynchronously. ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required