Manoj Rao bio photo

Manoj Rao

Your Average Common Man

Email Twitter Github

This post is not about staring into a distant sky ruminating about the future of C++. This is about the namesake features in the language.

If you are a C++ developer I am guessing you are familiar with Promises and Futures in C++, supported from C++11.

A future is an object that provides methods to access the data that needs to be communicated with a consumer. The semantics of the future are such that the access to the object can be done in different threads. The synchronization is provided by the language/compiler across multiple threads.

In it’s common form of usage, future objects are backed by a shared state object or an object that is to be shared between producer and consumer. future is the object that is typically associated with the shared object on the consumer side.

These future objects are instantiated in one of the following ways:

    async
    promise::get_future
    packaged_task::get_future

A promise is an object that can store the object which is to be shared by the producer. This shared object is typically retrieved into a future object. promise offers a synchronization mechanism on the producer side. The producer thread stores the shared object into a promise via the ::set_value() method.

        // instantiate a promise object to share
        // an object of string type
        auto prom = std::promise<std::string>();
        
        // a sample producer thread which sets
        // the shared object via set_value()
        auto producer = std::thread([&]
        {
            prom.set_value("Hello World");
        });
        
        // associate the shared object with a future
        // that can retrieve a value from some provider object,
        // in this case, a promise
        auto future = prom.get_future();
        
        // retrieve the shared object from the future
        auto consumer = std::thread([&]
        {
            std::cout << future.get();
        });
        
        // join threads
        producer.join();
        consumer.join();


Bring Your Own Cause

If you think any info here has remotely helped you consider dropping a penny for this cause, just click me . You can visit https://www.bbc.com/news/world-asia-india-52672764 Unfortunately, there are plenty of sad things happening all over the world, if you have a different cause or charity you'd rather support please do. And if you did make a donation, please drop a note to me (annotated) or leave a comment here (anonymous is OK!) and I will use that as motivation to write more useful content here.

If you like topics such as this then please consider subscribing to my podcast. I talk to some of the stalwarts in tech and ask them what their favorite productivity hacks are:

Available on iTunes Podcast

Visit Void Star Podcast’s page on iTunes Podcast Portal. Please Click ‘Subscribe’, leave a comment.

Get it iTunes