Imported Upstream version 1.63.0
[platform/upstream/boost.git] / libs / smart_ptr / extras / test / weak_ptr_mt_test.cpp
1 #include <boost/config.hpp>
2
3 #if defined(BOOST_MSVC)
4 #pragma warning(disable: 4786)  // identifier truncated in debug info
5 #pragma warning(disable: 4710)  // function not inlined
6 #pragma warning(disable: 4711)  // function selected for automatic inline expansion
7 #pragma warning(disable: 4514)  // unreferenced inline removed
8 #endif
9
10 //  weak_ptr_mt_test.cpp
11 //
12 //  Copyright (c) 2002 Peter Dimov and Multi Media Ltd.
13 //  Copyright 2005, 2008 Peter Dimov
14 //
15 //  Distributed under the Boost Software License, Version 1.0.
16 //  See accompanying file LICENSE_1_0.txt or copy at
17 //  http://www.boost.org/LICENSE_1_0.txt
18
19 #include <boost/shared_ptr.hpp>
20 #include <boost/weak_ptr.hpp>
21 #include <boost/bind.hpp>
22
23 #include <vector>
24
25 #include <cstdio>
26 #include <ctime>
27 #include <cstdlib>
28
29 #include <boost/detail/lightweight_thread.hpp>
30
31 //
32
33 int const n = 16384;
34 int const k = 512; // vector size
35 int const m = 16; // threads
36
37 void test( std::vector< boost::shared_ptr<int> > & v )
38 {
39     using namespace std; // printf, rand
40
41     std::vector< boost::weak_ptr<int> > w( v.begin(), v.end() );
42
43     int s = 0, f = 0, r = 0;
44
45     for( int i = 0; i < n; ++i )
46     {
47         // randomly kill a pointer
48
49         v[ rand() % k ].reset();
50         ++s;
51
52         for( int j = 0; j < k; ++j )
53         {
54             if( boost::shared_ptr<int> px = w[ j ].lock() )
55             {
56                 ++s;
57
58                 if( rand() & 4 )
59                 {
60                     continue;
61                 }
62
63                 // rebind anyway with prob. 50% for add_ref_lock() against weak_release() contention
64                 ++f;
65             }
66             else
67             {
68                 ++r;
69             }
70
71             w[ j ] = v[ rand() % k ];
72         }
73     }
74
75     printf( "\n%d locks, %d forced rebinds, %d normal rebinds.", s, f, r );
76 }
77
78 #if defined( BOOST_HAS_PTHREADS )
79
80 char const * thmodel = "POSIX";
81
82 #else
83
84 char const * thmodel = "Windows";
85
86 #endif
87
88 int main()
89 {
90     using namespace std; // printf, clock_t, clock
91
92     printf("Using %s threads: %d threads, %d * %d iterations: ", thmodel, m, n, k );
93
94     std::vector< boost::shared_ptr<int> > v( k );
95
96     for( int i = 0; i < k; ++i )
97     {
98         v[ i ].reset( new int( 0 ) );
99     }
100
101     clock_t t = clock();
102
103     pthread_t a[ m ];
104
105     for( int i = 0; i < m; ++i )
106     {
107         boost::detail::lw_thread_create( a[ i ], boost::bind( test, v ) );
108     }
109
110     v.resize( 0 ); // kill original copies
111
112     for( int j = 0; j < m; ++j )
113     {
114         pthread_join( a[j], 0 );
115     }
116
117     t = clock() - t;
118
119     printf("\n\n%.3f seconds.\n", static_cast<double>(t) / CLOCKS_PER_SEC);
120
121     return 0;
122 }