Imported Upstream version 1.63.0
[platform/upstream/boost.git] / libs / smart_ptr / extras / test / shared_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 //  shared_ptr_mt_test.cpp - tests shared_ptr with multiple threads
11 //
12 //  Copyright (c) 2002 Peter Dimov and Multi Media Ltd.
13 //  Copyright (c) 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/bind.hpp>
21
22 #include <vector>
23
24 #include <cstdio>
25 #include <ctime>
26
27 #include <boost/detail/lightweight_thread.hpp>
28
29 //
30
31 int const n = 1024 * 1024;
32
33 void test( boost::shared_ptr<int> const & pi )
34 {
35     std::vector< boost::shared_ptr<int> > v;
36
37     for( int i = 0; i < n; ++i )
38     {
39         v.push_back( pi );
40     }
41 }
42
43 int const m = 16; // threads
44
45 #if defined( BOOST_HAS_PTHREADS )
46
47 char const * thmodel = "POSIX";
48
49 #else
50
51 char const * thmodel = "Windows";
52
53 #endif
54
55 int main()
56 {
57     using namespace std; // printf, clock_t, clock
58
59     printf( "Using %s threads: %d threads, %d iterations: ", thmodel, m, n );
60
61     boost::shared_ptr<int> pi( new int(42) );
62
63     clock_t t = clock();
64
65     pthread_t a[ m ];
66
67     for( int i = 0; i < m; ++i )
68     {
69         boost::detail::lw_thread_create( a[ i ], boost::bind( test, pi ) );
70     }
71
72     for( int j = 0; j < m; ++j )
73     {
74         pthread_join( a[j], 0 );
75     }
76
77     t = clock() - t;
78
79     printf( "\n\n%.3f seconds.\n", static_cast<double>(t) / CLOCKS_PER_SEC );
80
81     return 0;
82 }