Вы можете использовать «классическое решение» для обмена данными между потоками: shared_ptr
.Я не думаю, что promise
и future
могут (обычно) использоваться для передачи частичных данных: «Обратите внимание, что объект std :: обещание предназначен для использования только один раз».(cppreference.com: здесь ).Я изменил ваш код (немного), чтобы показать мою точку зрения, пожалуйста, возвращайтесь, если есть проблемы:
#include "bits/stdc++.h"
using namespace std;
// only a string, but a structure is more general
struct Transfer {
std::string m_status;
Transfer() noexcept {};
Transfer(const std::string& status) : m_status(status)
{};
};
// std::atomic_shared_ptr is a (new) alternative to this use of std::shared_ptr
// warning: atomic functions for shared pointers do work on the shared
// pointer, not on the contained data
int sampleFunc(vector<future<int>> &f, std::shared_ptr<Transfer>& status) {
std::atomic_store(&status, std::make_shared<Transfer>("Waiting for the first result ..."));
int a = f[0].get();
std::this_thread::sleep_for(chrono::milliseconds(100)); // to "sync" the output
cout << "got a from promise 1" << endl;
std::atomic_store(&status, std::make_shared<Transfer>("Waiting for the second result ..."));
int b = f[1].get();
std::this_thread::sleep_for(chrono::milliseconds(100)); //to "sync" the output
cout << "got b from promise 2" << endl;
std::atomic_store(&status, std::make_shared<Transfer>("Finishing ..."));
std::this_thread::sleep_for(chrono::seconds(1));
return a + b;
}
int main() {
int outputVal;
int k = 2;
std::vector<promise<int>> p(k);
vector<future<int>> f(k);
f[0] = p[0].get_future();
f[1] = p[1].get_future();
std::shared_ptr<Transfer> status_shared = std::make_shared<Transfer>("Started");
// do not forget to use std::ref
std::future<int> fu = std::async(std::launch::async, sampleFunc, std::ref(f), std::ref(status_shared));
const auto wait_for_1 = std::async(std::launch::async, [](promise<int>& p) {
std::this_thread::sleep_for(chrono::milliseconds(1000));
p.set_value(2);
}, std::ref(p[0]));
const auto wait_for_2 = std::async(std::launch::async, [](promise<int>& p) {
std::this_thread::sleep_for(chrono::milliseconds(3000));
p.set_value(4);
}, std::ref(p[1]));
do {
const auto status = std::atomic_load(&status_shared);
cout << status->m_status << '\n';
} while (future_status::timeout == fu.wait_for(chrono::seconds(1)));
outputVal = fu.get();
cout << outputVal << endl;
return 0;
}