public member function
<future>
std::packaged_task::packaged_task default (1)packaged_task() noexcept;initialization (2)
template <class Fn> explicit packaged_task (Fn&& fn);with allocator (3)
template <class Fn, class Alloc> explicit packaged_task (allocator_arg_t aa, const Alloc& alloc, Fn&& fn);copy [deleted] (4)
packaged_task (packaged_task&) = delete;move (5)
packaged_task (packaged_task&& x) noexcept;default (1)
packaged_task() noexcept;initialization (2)
template <class Fn> explicit packaged_task (Fn&& fn);with allocator (3)
template <class Fn, class Alloc> explicit packaged_task (allocator_arg_t aa, const Alloc& alloc, Fn&& fn);copy [deleted] (4)
packaged_task (const packaged_task&) = delete;move (5)
packaged_task (packaged_task&& x) noexcept;
Construct packaged task
Constructs a packaged_task:std::forward<Fn>(fn)
).
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
// packaged_task construction / assignment
#include <iostream> // std::cout
#include <utility> // std::move
#include <future> // std::packaged_task, std::future
#include <thread> // std::thread
int main ()
{
std::packaged_task<int(int)> foo; // default-constructed
std::packaged_task<int(int)> bar ([](int x){return x*2;}); // initialized
foo = std::move(bar); // move-assignment
std::future<int> ret = foo.get_future(); // get future
std::thread(std::move(foo),10).detach(); // spawn thread and call task
// ...
int value = ret.get(); // wait for the task to finish and get result
std::cout << "The double of 10 is " << value << ".\n";
return 0;
}
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4