// (C) Copyright 2013 Ruslan Baratov // // Distributed under the Boost Software License, Version 1.0. (See accompanying // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) // See www.boost.org/libs/thread for documentation. #include #include #include #include void func_with_0_arg() { std::cout << __FUNCTION__ << std::endl; } void func_with_1_arg(int arg_1) { std::cout << __FUNCTION__ << ": " << arg_1 << std::endl; } bool func_with_2_arg(int arg_1, bool arg_2) { std::cout << __FUNCTION__ << ": " << arg_1 << ", " << arg_2 << std::endl; return !arg_2; } int func_with_3_arg(int arg_1, bool arg_2, const char* arg_3) { std::cout << __FUNCTION__ << ": " << arg_1 << ", " << arg_2 << ", "; std::cout << "<" << arg_3 << ">" << std::endl; return 12; } const char* func_with_4_arg(int arg_1, bool arg_2, int* arg_3, int& arg_4) { std::cout << __FUNCTION__ << ": " << arg_1 << ", " << arg_2 << std::endl; *arg_3 = 128; arg_4 = 456; return "hello"; } void example() { boost::mutex m; boost::with_lock_guard(m, func_with_0_arg); boost::with_lock_guard(m, func_with_1_arg, 3); bool res2 = boost::with_lock_guard(m, func_with_2_arg, 3, true); std::cout << "res2 = " << res2 << std::endl; int arg1 = 13; const char* mes = "message for func with 3 arg"; int res3 = boost::with_lock_guard(m, func_with_3_arg, arg1, false, mes); std::cout << "res3 = " << res3 << std::endl; int arg3 = 0; int arg4 = 0; const char* res4 = boost::with_lock_guard( m, func_with_4_arg, 23, false, &arg3, boost::ref(arg4) ); std::cout << "res4 = <" << res4 << ">, arg3 = " << arg3 << ", arg4 = " << arg4 << std::endl; } class Foo { public: Foo(int value): value_(value) { } int func(int a, int b) { return a + b + value_; } int func_ref(int& a) { a = 133; return 34; } private: int value_; }; void example_bind() { boost::mutex m; Foo foo(2); int res_bind = boost::with_lock_guard( m, boost::bind(&Foo::func, foo, 1, 31) ); std::cout << "res_bind = " << res_bind << std::endl; int a = 0; int res_bind_ref = boost::with_lock_guard( m, boost::bind(&Foo::func_ref, foo, boost::ref(a)) ); std::cout << "res_bind_ref = " << res_bind_ref << ", " << a << std::endl; } #if defined(BOOST_NO_CXX11_VARIADIC_TEMPLATES) void example_variadic_templates() { std::cout << "C++11 variadic templates disabled" << std::endl; } void example_lambda() { std::cout << "C++11 lambda disabled" << std::endl; } #else int func_with_5_args(int a1, char a2, int& a3, bool* a4, bool a5) { std::cout << __FUNCTION__ << ": " << a1 << ", <" << a2 << ">, " << a5 << std::endl; a3 = 135; *a4 = false; return 45; } int func_with_6_args(int a1, char a2, int& a3, bool* a4, int&& a5, bool a6) { std::cout << __FUNCTION__ << ": " << a1 << ", <" << a2 << ">, " << a5 << ", " << a6 << std::endl; a3 = 200; *a4 = true; return 888; } void example_variadic_templates() { boost::mutex m; int a3 = 0; bool a4 = true; int res5 = boost::with_lock_guard( m, func_with_5_args, 12, 'x', a3, &a4, false ); std::cout << "res5 = " << res5 << ", " << a3 << ", " << a4 << std::endl; int res6 = boost::with_lock_guard( m, func_with_6_args, 12, 'N', a3, &a4, 2, false ); std::cout << "res6 = " << res6 << ", " << a3 << ", " << a4 << std::endl; int a5 = 13; int res6_move = boost::with_lock_guard( m, func_with_6_args, 12, 'N', a3, &a4, std::move(a5), false ); std::cout << "res6(move) = " << res6_move << ", " << a3 << ", " << a4 << std::endl; } void example_lambda() { boost::mutex m; int res_lambda = boost::with_lock_guard( m, [](int a) { return a + 3; }, 13 ); std::cout << "res lambda = " << res_lambda << std::endl; } #endif int main() { std::cout << std::boolalpha; example(); example_bind(); example_lambda(); example_variadic_templates(); return 0; }