John
John

Reputation: 3494

Question about `std::packaged_task<R(Args...)>::get_future`

As per the document, which says that[emphasise mine]:

std::future get_future(); (since C++11)

Returns a future which shares the same shared state as *this.

get_future can be called only once for each packaged_task.

1.What's the *this at here? The instance of std::packaged_task<R(Args...)>? Could somebody shed some light on this matter?

2.Must I call get_future() (in the current thread) before the packaged_task is invoked(by another thread)? Should I pay attention to any possible race condition?

Upvotes: 0

Views: 53

Answers (1)

Caglayan Dokme
Caglayan Dokme

Reputation: 1158

From this page, I understood that the *this refers to the task instance of std::packaged_task<int()>. Therefore, your assumption was true.

// future from a packaged_task
std::packaged_task<int()> task([]{ return 7; });    // wrap the function
std::future<int> f1 = task.get_future();            // get a future
std::thread t(std::move(task));                     // launch on a thread

Upvotes: 1

Related Questions