1// (C) Copyright 2013 Andrey
2// (C) Copyright 2013 Vicente J. Botet Escriba
3//
4// Distributed under the Boost Software License, Version 1.0. (See
5// accompanying file LICENSE_1_0.txt or copy at
6// http://www.boost.org/LICENSE_1_0.txt)
7//
8// This performance test is based on the performance test provided by maxim.yegorushkin
9// at https://svn.boost.org/trac/boost/ticket/7422
10
11#define BOOST_THREAD_USES_CHRONO
12
13#include <iostream>
14#include <boost/thread/lock_types.hpp>
15#include <boost/thread/thread_only.hpp>
16#include <boost/chrono/chrono_io.hpp>
17
18#include <boost/thread/shared_mutex.hpp>
19
20using namespace boost;
21
22shared_mutex mtx;
23const int cycles = 10000;
24
25void shared()
26{
27 int cycle(0);
28 while (++cycle < cycles)
29 {
30 shared_lock<shared_mutex> lock(mtx);
31 }
32}
33
34void unique()
35{
36 int cycle(0);
37 while (++cycle < cycles)
38 {
39 unique_lock<shared_mutex> lock(mtx);
40 }
41}
42
43int main()
44{
45 boost::chrono::high_resolution_clock::duration best_time(std::numeric_limits<boost::chrono::high_resolution_clock::duration::rep>::max BOOST_PREVENT_MACRO_SUBSTITUTION ());
46 for (int i =100; i>0; --i) {
47 boost::chrono::high_resolution_clock clock;
48 boost::chrono::high_resolution_clock::time_point s1 = clock.now();
49 thread t0(shared);
50 thread t1(shared);
51 thread t2(unique);
52 //thread t11(shared);
53 //thread t12(shared);
54 //thread t13(shared);
55
56 t0.join();
57 t1.join();
58 t2.join();
59 //t11.join();
60// t12.join();
61 // t13.join();
62 boost::chrono::high_resolution_clock::time_point f1 = clock.now();
63 //std::cout << " Time spent:" << (f1 - s1) << std::endl;
64 best_time = std::min BOOST_PREVENT_MACRO_SUBSTITUTION (a: best_time, b: f1 - s1);
65
66 }
67 std::cout << "Best Time spent:" << best_time << std::endl;
68 std::cout << "Time spent/cycle:" << best_time/cycles/3 << std::endl;
69
70 return 1;
71}
72
73

source code of boost/libs/thread/example/perf_shared_mutex.cpp