std::promise and std::future as pointer

Mar 28, 2018 at 3:53pm
I want to make std::promise and std::future as pointers and make it a varibles of myClass

class myclass {

std::future
std::promise * prom
}

I am not sure now to new and of promise and future and how to get future from pointer to promise

Can someone helping in writing the code
Mar 28, 2018 at 5:35pm
That seems to be even less meaningful than a pointer to std::list, but sure:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include <memory>
#include <future>
#include <thread>
struct myclass {
  std::unique_ptr<std::promise<void>> p; // promise before future
  std::unique_ptr<std::future<void>> f;
  myclass() : p{std::make_unique<std::promise<void>>()},
              f{std::make_unique<std::future<void>>(p->get_future())} {}
};    
int main () {
    myclass mc;
    std::thread t{[p = std::move(mc.p)]{ p->set_value(); }};
    mc.f->wait();
    t.join();
}

live demo https://wandbox.org/permlink/NuUirMvILIW5f7h9
Last edited on Mar 28, 2018 at 5:37pm
Topic archived. No new replies allowed.