-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPackagedTask.cpp
59 lines (49 loc) · 1.78 KB
/
PackagedTask.cpp
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
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
/**
* \file PackagedTask.cpp
* \brief
*
* \see http://en.cppreference.com/w/cpp/thread/packaged_task
* \review
*/
#include <StdStream/StdStream.h>
#include <StdTest/StdTest.h>
#include <Stl.h>
//--------------------------------------------------------------------------------------------------
// unique function to avoid disambiguating the std::pow overload set
int f(int x, int y) { return std::pow(x,y); }
//--------------------------------------------------------------------------------------------------
void task_lambda()
{
std::packaged_task<int(int,int)> task([](int a, int b) { return std::pow(a, b); });
std::future<int> result = task.get_future();
task(2, 9);
std::cout << "task_lambda:\t" << result.get() << '\n';
}
//--------------------------------------------------------------------------------------------------
void task_bind()
{
std::packaged_task<int()> task(std::bind(f, 2, 11));
std::future<int> result = task.get_future();
task();
std::cout << "task_bind:\t" << result.get() << '\n';
}
//--------------------------------------------------------------------------------------------------
void task_thread()
{
/* the following does not work */
//std::packaged_task<int(int,int)> task(f);
std::packaged_task<int(int,int)> task([](int a, int b) { return f(a,b); });
std::future<int> result = task.get_future();
std::thread task_td(std::move(task), 2, 10);
task_td.join();
std::cout << "task_thread:\t" << result.get() << '\n';
}
//--------------------------------------------------------------------------------------------------
int main(int, char **)
{
task_lambda();
task_bind();
task_thread();
return 0;
}
//--------------------------------------------------------------------------------------------------