1 // (C) Copyright 2013 Ruslan Baratov
2 // (C) Copyright 2013 Ruslan Baratov
4 // Distributed under the Boost Software License, Version 1.0. (See accompanying
5 // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
7 // See www.boost.org/libs/thread for documentation.
9 #define BOOST_THREAD_VERSION 4
11 #include <boost/detail/lightweight_test.hpp> // BOOST_TEST
13 #include <boost/thread/mutex.hpp>
14 #include <boost/thread/with_lock_guard.hpp>
15 #include <boost/ref.hpp>
17 void func_with_0_arg() {
20 void func_with_1_arg(int arg_1) {
21 BOOST_TEST(arg_1 == 3);
24 bool func_with_2_arg(int arg_1, bool arg_2) {
25 BOOST_TEST(arg_1 == 3);
26 BOOST_TEST(arg_2 == true);
30 int func_with_3_arg(int arg_1, bool arg_2, const char* arg_3) {
31 BOOST_TEST(arg_1 == 13);
32 BOOST_TEST(arg_2 == false);
33 BOOST_TEST(std::string(arg_3) == "message for func with 3 arg");
37 const char* func_with_4_arg(int arg_1, bool arg_2, int* arg_3, int& arg_4) {
38 BOOST_TEST(arg_1 == 23);
39 BOOST_TEST(arg_2 == false);
49 boost::with_lock_guard(m, func_with_0_arg);
52 boost::with_lock_guard(m, func_with_1_arg, 3);
55 bool res2 = boost::with_lock_guard(m, func_with_2_arg, 3, true);
56 BOOST_TEST(res2 == false);
60 const char* mes = "message for func with 3 arg";
61 int res3 = boost::with_lock_guard(m, func_with_3_arg, arg1, false, mes);
62 BOOST_TEST(res3 == 12);
67 const char* res4 = boost::with_lock_guard(
75 BOOST_TEST(arg3 == 128);
76 BOOST_TEST(arg4 == 456);
77 BOOST_TEST(std::string(res4) == "hello");
80 #if defined(BOOST_NO_CXX11_VARIADIC_TEMPLATES)
81 void test_variadic_templates() {
82 std::cout << "C++11 variadic templates disabled" << std::endl;
85 int func_with_5_args(int a1, char a2, int& a3, bool* a4, bool a5) {
87 BOOST_TEST(a2 == 'x');
88 BOOST_TEST(a5 == false);
94 int func_with_6_args(int a1, char a2, int& a3, bool* a4, int&& a5, bool a6) {
96 BOOST_TEST(a2 == 'N');
97 BOOST_TEST(a5 == 2 || a5 == 13);
98 BOOST_TEST(a6 == false);
104 void test_variadic_templates() {
109 int res5 = boost::with_lock_guard(
110 m, func_with_5_args, 12, 'x', a3, &a4, false
112 BOOST_TEST(a3 == 135);
113 BOOST_TEST(a4 == false);
114 BOOST_TEST(res5 == 45);
116 int res6 = boost::with_lock_guard(
117 m, func_with_6_args, 12, 'N', a3, &a4, 2, false
119 BOOST_TEST(a3 == 200);
120 BOOST_TEST(a4 == true);
121 BOOST_TEST(res6 == 888);
126 int res6_move = boost::with_lock_guard(
127 m, func_with_6_args, 12, 'N', a3, &a4, boost::move(a5), false
129 BOOST_TEST(a3 == 200);
130 BOOST_TEST(a4 == true);
131 BOOST_TEST_EQ(res6_move, 888);
137 test_variadic_templates();
138 return boost::report_errors();