User Tools

Site Tools


Differences

This shows you the differences between two versions of the page.

Link to this comparison view

cpp:prom [2019-04-05 10:53] (current)
ziggi created
Line 1: Line 1:
 +<code c++ 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;
 +}
 +</​code>​