C++ Library Extensions 2022.12.09
To help learn modern C++ programming
010-thread_function.cpp
Go to the documentation of this file.
1#include <tpf_output.hpp>
2#include <future>
3#include <thread>
4
5
6int global_sum(int a, int b)
7{
8 return a + b;
9}
10
11class SquareSum
12{
13 public:
14 int square_sum(int a, int b)
15 {
16 return a * a + b * b;
17 }
18
19 static int sum_square(int a, int b)
20 {
21 return (a+b)*(a+b);
22 }
23
24 // do not forget trailing const specifier
25 // in some case, if you do not use const specifier
26 // your compiler may complain or refuse compiling at all
27 int operator()(int n) const
28 {
29 return n*n + n*n;
30 }
31};
32
33
35{
36 try
37 {
38 /*
39 Always put your multithread code in
40 try - catch construct
41 */
42
43 // using global function as thread function
44 auto global_thread_func = std::async(global_sum, 2, 3);
45
46 SquareSum square_sum_object;
47
48 // using non-static member function as thread function
49 auto member_square_sum_fun = std::async( &SquareSum::square_sum, // member function's function pointer - relative address
50 &square_sum_object, // the object that contains member function square_sum
51 3, 4 // arguments for SquareSum::square_sum(a, b)
52 );
53
54 // using static member function as thread function
55 auto static_member_function = std::async(&SquareSum::sum_square, 2, 3);
56
57 // using functor as a thread function
58 // "by functor" I mean a class or struct
59 // that defined operator()()
60 auto functor_thread_func = std::async(SquareSum{}, 5);
61
62 // using lambda as a thread function
63 auto lambda_thread_func = std::async([](int a, int b, int c)
64 { return a*a + b*b + c*c; }, 1, 2, 3);
65
67
68 stream << "global_thread_func - sum of 2, 3: " << global_thread_func.get() << tpf::endl;
69 stream << "member_square_sum_fun - square_sum of 3, 4: " << member_square_sum_fun.get() << tpf::endl;
70 stream << "static_member_function - sum_square of 3, 4: " << static_member_function.get() << tpf::endl;
71 stream << "functor_thread_func - square sum of 5: " << functor_thread_func.get() << tpf::endl;
72 stream << "lambda_thread_func - sum of squares if 1, 2, 3 :" << lambda_thread_func.get() << tpf::endl;
73 }
74 catch(const std::exception& e)
75 {
77
78 stream << e << tpf::endl;
79 }
80
81}
82
84{
85 try
86 {
87 /*
88 Always put your multithread code in
89 try - catch construct
90 */
91
92 // using global function as thread function
93 auto global_thread_func = std::async(std::launch::async, global_sum, 2, 3);
94
95 SquareSum square_sum_object;
96
97 // using non-static member function as thread function
98 auto member_square_sum_fun = std::async(std::launch::async, &SquareSum::square_sum, // member function's function pointer - relative address
99 &square_sum_object, // the object that contains member function square_sum
100 3, 4 // arguments for SquareSum::square_sum(a, b)
101 );
102
103 // using static member function as thread function
104 auto static_member_function = std::async(std::launch::async, &SquareSum::sum_square, 2, 3);
105
106 // using functor as a thread function
107 // "by functor" I mean a class or struct
108 // that defined operator()()
109 auto functor_thread_func = std::async(std::launch::async, SquareSum{}, 5);
110
111 // using lambda as a thread function
112 auto lambda_thread_func = std::async(std::launch::async, [](int a, int b, int c)
113 { return a*a + b*b + c*c; }, 1, 2, 3);
114
116
117 stream << "global_thread_func - sum of 2, 3: " << global_thread_func.get() << tpf::endl;
118 stream << "member_square_sum_fun - square_sum of 3, 4: " << member_square_sum_fun.get() << tpf::endl;
119 stream << "static_member_function - sum_square of 3, 4: " << static_member_function.get() << tpf::endl;
120 stream << "functor_thread_func - square sum of 5: " << functor_thread_func.get() << tpf::endl;
121 stream << "lambda_thread_func - sum of squares if 1, 2, 3 :" << lambda_thread_func.get() << tpf::endl;
122 }
123 catch(const std::exception& e)
124 {
126
127 stream << e << tpf::endl;
128 }
129
130}
131
132
133int main()
134{
135 // example_for_thread_functions();
137}
void example_for_thread_functions_async()
void example_for_thread_functions()
int global_sum(int a, int b)
int main()
tpf::sstream stream
int operator()(int n) const
static int sum_square(int a, int b)
int square_sum(int a, int b)
constexpr auto endl
Definition: tpf_output.hpp:973
Stream output operators << are implemented.