| 
									
										
										
										
											2005-04-02 19:35:58 +00:00
										 |  |  | //  weak_ptr_mt_test.cpp
 | 
					
						
							|  |  |  | //
 | 
					
						
							|  |  |  | //  Copyright (c) 2002 Peter Dimov and Multi Media Ltd.
 | 
					
						
							| 
									
										
										
										
											2008-07-12 10:41:24 +00:00
										 |  |  | //  Copyright 2005, 2008 Peter Dimov
 | 
					
						
							| 
									
										
										
										
											2005-04-02 19:35:58 +00:00
										 |  |  | //
 | 
					
						
							| 
									
										
										
										
											2008-07-12 10:41:24 +00:00
										 |  |  | //  Distributed under the Boost Software License, Version 1.0.
 | 
					
						
							|  |  |  | //  See accompanying file LICENSE_1_0.txt or copy at
 | 
					
						
							|  |  |  | //  http://www.boost.org/LICENSE_1_0.txt
 | 
					
						
							| 
									
										
										
										
											2005-04-02 19:35:58 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | #include <boost/shared_ptr.hpp>
 | 
					
						
							|  |  |  | #include <boost/weak_ptr.hpp>
 | 
					
						
							|  |  |  | #include <boost/bind.hpp>
 | 
					
						
							| 
									
										
										
										
											2024-09-24 18:25:13 +03:00
										 |  |  | #include <boost/config.hpp>
 | 
					
						
							| 
									
										
										
										
											2005-04-02 19:35:58 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-09-24 18:25:13 +03:00
										 |  |  | #include <boost/smart_ptr/detail/lightweight_thread.hpp>
 | 
					
						
							| 
									
										
										
										
											2005-04-02 19:35:58 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-09-24 18:25:13 +03:00
										 |  |  | #include <vector>
 | 
					
						
							| 
									
										
										
										
											2005-04-02 19:35:58 +00:00
										 |  |  | #include <cstdio>
 | 
					
						
							|  |  |  | #include <ctime>
 | 
					
						
							|  |  |  | #include <cstdlib>
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | //
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | int const n = 16384; | 
					
						
							|  |  |  | int const k = 512; // vector size
 | 
					
						
							|  |  |  | int const m = 16; // threads
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | void test( std::vector< boost::shared_ptr<int> > & v ) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     using namespace std; // printf, rand
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     std::vector< boost::weak_ptr<int> > w( v.begin(), v.end() ); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     int s = 0, f = 0, r = 0; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     for( int i = 0; i < n; ++i ) | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         // randomly kill a pointer
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         v[ rand() % k ].reset(); | 
					
						
							|  |  |  |         ++s; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         for( int j = 0; j < k; ++j ) | 
					
						
							|  |  |  |         { | 
					
						
							|  |  |  |             if( boost::shared_ptr<int> px = w[ j ].lock() ) | 
					
						
							|  |  |  |             { | 
					
						
							|  |  |  |                 ++s; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |                 if( rand() & 4 ) | 
					
						
							|  |  |  |                 { | 
					
						
							|  |  |  |                     continue; | 
					
						
							|  |  |  |                 } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |                 // rebind anyway with prob. 50% for add_ref_lock() against weak_release() contention
 | 
					
						
							|  |  |  |                 ++f; | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |             else | 
					
						
							|  |  |  |             { | 
					
						
							|  |  |  |                 ++r; | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             w[ j ] = v[ rand() % k ]; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     printf( "\n%d locks, %d forced rebinds, %d normal rebinds.", s, f, r ); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-07-12 10:41:24 +00:00
										 |  |  | #if defined( BOOST_HAS_PTHREADS )
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | char const * thmodel = "POSIX"; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #else
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | char const * thmodel = "Windows"; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #endif
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2005-04-02 19:35:58 +00:00
										 |  |  | int main() | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     using namespace std; // printf, clock_t, clock
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-07-12 10:41:24 +00:00
										 |  |  |     printf("Using %s threads: %d threads, %d * %d iterations: ", thmodel, m, n, k ); | 
					
						
							| 
									
										
										
										
											2005-04-02 19:35:58 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |     std::vector< boost::shared_ptr<int> > v( k ); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     for( int i = 0; i < k; ++i ) | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         v[ i ].reset( new int( 0 ) ); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     clock_t t = clock(); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-03-08 01:19:44 +02:00
										 |  |  |     boost::detail::lw_thread_t a[ m ]; | 
					
						
							| 
									
										
										
										
											2005-04-02 19:35:58 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-07-12 10:41:24 +00:00
										 |  |  |     for( int i = 0; i < m; ++i ) | 
					
						
							| 
									
										
										
										
											2005-04-02 19:35:58 +00:00
										 |  |  |     { | 
					
						
							| 
									
										
										
										
											2008-07-12 10:41:24 +00:00
										 |  |  |         boost::detail::lw_thread_create( a[ i ], boost::bind( test, v ) ); | 
					
						
							| 
									
										
										
										
											2005-04-02 19:35:58 +00:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     v.resize( 0 ); // kill original copies
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-07-12 10:41:24 +00:00
										 |  |  |     for( int j = 0; j < m; ++j ) | 
					
						
							| 
									
										
										
										
											2005-04-02 19:35:58 +00:00
										 |  |  |     { | 
					
						
							| 
									
										
										
										
											2018-03-08 01:19:44 +02:00
										 |  |  |         boost::detail::lw_thread_join( a[j] ); | 
					
						
							| 
									
										
										
										
											2005-04-02 19:35:58 +00:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     t = clock() - t; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     printf("\n\n%.3f seconds.\n", static_cast<double>(t) / CLOCKS_PER_SEC); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     return 0; | 
					
						
							|  |  |  | } |