1 | // (C) Copyright 2013 Ruslan Baratov
|
---|
2 | //
|
---|
3 | // Distributed under the Boost Software License, Version 1.0. (See accompanying
|
---|
4 | // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
|
---|
5 |
|
---|
6 | // See www.boost.org/libs/thread for documentation.
|
---|
7 |
|
---|
8 | #include <iostream>
|
---|
9 | #include <boost/bind.hpp>
|
---|
10 | #include <boost/thread/mutex.hpp>
|
---|
11 | #include <boost/thread/with_lock_guard.hpp>
|
---|
12 |
|
---|
13 | void func_with_0_arg() {
|
---|
14 | std::cout << __FUNCTION__ << std::endl;
|
---|
15 | }
|
---|
16 |
|
---|
17 | void func_with_1_arg(int arg_1) {
|
---|
18 | std::cout << __FUNCTION__ << ": " << arg_1 << std::endl;
|
---|
19 | }
|
---|
20 |
|
---|
21 | bool func_with_2_arg(int arg_1, bool arg_2) {
|
---|
22 | std::cout << __FUNCTION__ << ": " << arg_1 << ", " << arg_2 << std::endl;
|
---|
23 | return !arg_2;
|
---|
24 | }
|
---|
25 |
|
---|
26 | int func_with_3_arg(int arg_1, bool arg_2, const char* arg_3) {
|
---|
27 | std::cout << __FUNCTION__ << ": " << arg_1 << ", " << arg_2 << ", ";
|
---|
28 | std::cout << "<" << arg_3 << ">" << std::endl;
|
---|
29 | return 12;
|
---|
30 | }
|
---|
31 |
|
---|
32 | const char* func_with_4_arg(int arg_1, bool arg_2, int* arg_3, int& arg_4) {
|
---|
33 | std::cout << __FUNCTION__ << ": " << arg_1 << ", " << arg_2 << std::endl;
|
---|
34 | *arg_3 = 128;
|
---|
35 | arg_4 = 456;
|
---|
36 | return "hello";
|
---|
37 | }
|
---|
38 |
|
---|
39 | void example() {
|
---|
40 | boost::mutex m;
|
---|
41 |
|
---|
42 | boost::with_lock_guard(m, func_with_0_arg);
|
---|
43 | boost::with_lock_guard(m, func_with_1_arg, 3);
|
---|
44 | bool res2 = boost::with_lock_guard(m, func_with_2_arg, 3, true);
|
---|
45 | std::cout << "res2 = " << res2 << std::endl;
|
---|
46 |
|
---|
47 | int arg1 = 13;
|
---|
48 | const char* mes = "message for func with 3 arg";
|
---|
49 | int res3 = boost::with_lock_guard(m, func_with_3_arg, arg1, false, mes);
|
---|
50 | std::cout << "res3 = " << res3 << std::endl;
|
---|
51 |
|
---|
52 | int arg3 = 0;
|
---|
53 | int arg4 = 0;
|
---|
54 | const char* res4 = boost::with_lock_guard(
|
---|
55 | m,
|
---|
56 | func_with_4_arg,
|
---|
57 | 23,
|
---|
58 | false,
|
---|
59 | &arg3,
|
---|
60 | boost::ref(arg4)
|
---|
61 | );
|
---|
62 | std::cout << "res4 = <" << res4 << ">, arg3 = " << arg3 <<
|
---|
63 | ", arg4 = " << arg4 << std::endl;
|
---|
64 | }
|
---|
65 |
|
---|
66 | class Foo {
|
---|
67 | public:
|
---|
68 | Foo(int value): value_(value) {
|
---|
69 | }
|
---|
70 |
|
---|
71 | int func(int a, int b) {
|
---|
72 | return a + b + value_;
|
---|
73 | }
|
---|
74 |
|
---|
75 | int func_ref(int& a) {
|
---|
76 | a = 133;
|
---|
77 | return 34;
|
---|
78 | }
|
---|
79 |
|
---|
80 | private:
|
---|
81 | int value_;
|
---|
82 | };
|
---|
83 |
|
---|
84 | void example_bind() {
|
---|
85 | boost::mutex m;
|
---|
86 | Foo foo(2);
|
---|
87 |
|
---|
88 | int res_bind = boost::with_lock_guard(
|
---|
89 | m,
|
---|
90 | boost::bind(&Foo::func, foo, 1, 31)
|
---|
91 | );
|
---|
92 | std::cout << "res_bind = " << res_bind << std::endl;
|
---|
93 |
|
---|
94 | int a = 0;
|
---|
95 | int res_bind_ref = boost::with_lock_guard(
|
---|
96 | m,
|
---|
97 | boost::bind(&Foo::func_ref, foo, boost::ref(a))
|
---|
98 | );
|
---|
99 | std::cout << "res_bind_ref = " << res_bind_ref << ", " << a << std::endl;
|
---|
100 | }
|
---|
101 |
|
---|
102 | #if defined(BOOST_NO_CXX11_VARIADIC_TEMPLATES)
|
---|
103 | void example_variadic_templates() {
|
---|
104 | std::cout << "C++11 variadic templates disabled" << std::endl;
|
---|
105 | }
|
---|
106 |
|
---|
107 | void example_lambda() {
|
---|
108 | std::cout << "C++11 lambda disabled" << std::endl;
|
---|
109 | }
|
---|
110 | #else
|
---|
111 | int func_with_5_args(int a1, char a2, int& a3, bool* a4, bool a5) {
|
---|
112 | std::cout << __FUNCTION__ << ": " <<
|
---|
113 | a1 << ", <" << a2 << ">, " << a5 << std::endl;
|
---|
114 | a3 = 135;
|
---|
115 | *a4 = false;
|
---|
116 | return 45;
|
---|
117 | }
|
---|
118 |
|
---|
119 | int func_with_6_args(int a1, char a2, int& a3, bool* a4, int&& a5, bool a6) {
|
---|
120 | std::cout << __FUNCTION__ << ": " << a1 << ", <" << a2 << ">, " <<
|
---|
121 | a5 << ", " << a6 << std::endl;
|
---|
122 | a3 = 200;
|
---|
123 | *a4 = true;
|
---|
124 | return 888;
|
---|
125 | }
|
---|
126 |
|
---|
127 | void example_variadic_templates() {
|
---|
128 | boost::mutex m;
|
---|
129 |
|
---|
130 | int a3 = 0;
|
---|
131 | bool a4 = true;
|
---|
132 | int res5 = boost::with_lock_guard(
|
---|
133 | m, func_with_5_args, 12, 'x', a3, &a4, false
|
---|
134 | );
|
---|
135 | std::cout << "res5 = " << res5 << ", " << a3 << ", " << a4 << std::endl;
|
---|
136 |
|
---|
137 | int res6 = boost::with_lock_guard(
|
---|
138 | m, func_with_6_args, 12, 'N', a3, &a4, 2, false
|
---|
139 | );
|
---|
140 | std::cout << "res6 = " << res6 << ", " << a3 << ", " << a4 << std::endl;
|
---|
141 |
|
---|
142 | int a5 = 13;
|
---|
143 | int res6_move = boost::with_lock_guard(
|
---|
144 | m, func_with_6_args, 12, 'N', a3, &a4, std::move(a5), false
|
---|
145 | );
|
---|
146 | std::cout << "res6(move) = " << res6_move << ", " <<
|
---|
147 | a3 << ", " << a4 << std::endl;
|
---|
148 | }
|
---|
149 |
|
---|
150 | void example_lambda() {
|
---|
151 | boost::mutex m;
|
---|
152 | int res_lambda = boost::with_lock_guard(
|
---|
153 | m,
|
---|
154 | [](int a) {
|
---|
155 | return a + 3;
|
---|
156 | },
|
---|
157 | 13
|
---|
158 | );
|
---|
159 | std::cout << "res lambda = " << res_lambda << std::endl;
|
---|
160 | }
|
---|
161 | #endif
|
---|
162 |
|
---|
163 | int main() {
|
---|
164 | std::cout << std::boolalpha;
|
---|
165 | example();
|
---|
166 | example_bind();
|
---|
167 | example_lambda();
|
---|
168 | example_variadic_templates();
|
---|
169 | return 0;
|
---|
170 | }
|
---|