User Tools

Site Tools


prom.cpp
#include <iostream>
#include <functional>
#include <thread>
#include <future>
 
void producer_set(std::promise<std::string>& promise) {
    promise.set_value("Hello World");
}
 
void consumer_get(std::future<std::string>& future){
    std::cout << future.get() << std::endl;
};
 
int main(int argc, char **argv) {
    try {
        auto promise = std::promise<std::string>();
        std::future<std::string> future = promise.get_future();
 
        auto producer = std::thread(producer_set, std::ref(promise));
        auto consumer = std::thread(consumer_get, std::ref(future));
 
        consumer.join();
        producer.join();
 
    } catch (std::exception& e) {
        std::cerr << "exeption: " << e.what() << ", exit." << std::endl;
        return 1;
    } catch (...) {
        std::cerr << "generic exeption, exit." << std::endl;
        return 1;
    }
    return 0;
}