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

Ale*_*lex 5 c++ concurrency multithreading promise c++11

我可以多次使用相同的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);
                }
            });
Run Code Online (Sandbox Code Playgroud)