'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?
Solution 1:[1]
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
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Caglayan DOKME |
