1 // (C) Copyright 2013 Ruslan Baratov
2 // Copyright (C) 2014 Vicente J. Botet Escriba
3 //
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)
6 
7 //  See www.boost.org/libs/thread for documentation.
8 
9 #include <boost/config.hpp>
10 
11 #if !defined(BOOST_NO_CXX11_DECLTYPE)
12 # define BOOST_RESULT_OF_USE_DECLTYPE
13 #endif
14 
15 #define BOOST_THREAD_VERSION 4
16 
17 #include <boost/detail/lightweight_test.hpp> // BOOST_TEST
18 
19 #include <iostream> // std::cout
20 #include <boost/thread/mutex.hpp>
21 #include <boost/thread/with_lock_guard.hpp>
22 
23 #if defined(BOOST_NO_CXX11_LAMBDAS) || (defined BOOST_MSVC && _MSC_VER < 1700)
test_lambda()24 void test_lambda() {
25   std::cout << "C++11 lambda disabled" << std::endl;
26 }
27 #else
test_lambda()28 void test_lambda() {
29   boost::mutex m;
30   int res_1 = boost::with_lock_guard(
31       m,
32       [](int a) {
33         BOOST_TEST(a == 13);
34         return a + 3;
35       },
36       13
37   );
38   BOOST_TEST(res_1 == 16);
39 
40   int v = 0;
41   int res_2 = boost::with_lock_guard(
42       m,
43       [&v](int a) {
44         BOOST_TEST(a == 55);
45         v = 15;
46         return 45;
47       },
48       55
49   );
50   BOOST_TEST(res_2 == 45);
51   BOOST_TEST(v == 15);
52 }
53 #endif
54 
main()55 int main() {
56   std::cout << std::boolalpha;
57   test_lambda();
58   return boost::report_errors();
59 }
60