我可以多次使用相同的std :: promise和std :: future对象吗?

时间:2019-03-07 13:54:48

标签: c++ multithreading c++11 concurrency promise

我可以多次使用相同的std::promisestd::future对象吗?

例如,我想多次从Thread-1到Thread-2发送多个值。我是否可以多次使用promise / future,以及如何做到线程安全?

            std::promise<int> send_value;
            std::future<int> receive_value = send_value.get_future();

            std::thread t1 = std::thread([&]()
            {
                while (!exit_flag) {
                    int value = my_custom_function_1();
                    send_value.set_value(value);
                }
            });

            std::thread t2 = std::thread([&]()
            {
                while (!exit_flag) {
                    int value = receive_value.get();
                    my_custom_function_2(value);
                }
            });

0 个答案:

没有答案
相关问题