| 
									
										
										
										
											2003-11-28 15:35:21 +00:00
										 |  |  | #include <boost/config.hpp>
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #if defined(BOOST_MSVC)
 | 
					
						
							| 
									
										
										
										
											2002-02-16 16:09:08 +00:00
										 |  |  | #pragma warning(disable: 4786)  // identifier truncated in debug info
 | 
					
						
							|  |  |  | #pragma warning(disable: 4710)  // function not inlined
 | 
					
						
							|  |  |  | #pragma warning(disable: 4711)  // function selected for automatic inline expansion
 | 
					
						
							|  |  |  | #pragma warning(disable: 4514)  // unreferenced inline removed
 | 
					
						
							|  |  |  | #endif
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | //  shared_ptr_mt_test.cpp - tests shared_ptr with multiple threads
 | 
					
						
							|  |  |  | //
 | 
					
						
							|  |  |  | //  Copyright (c) 2002 Peter Dimov and Multi Media Ltd.
 | 
					
						
							| 
									
										
										
										
											2008-07-12 10:41:24 +00:00
										 |  |  | //  Copyright (c) 2008 Peter Dimov
 | 
					
						
							| 
									
										
										
										
											2002-02-16 16:09:08 +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
 | 
					
						
							| 
									
										
										
										
											2002-02-16 16:09:08 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | #include <boost/shared_ptr.hpp>
 | 
					
						
							|  |  |  | #include <boost/bind.hpp>
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #include <vector>
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #include <cstdio>
 | 
					
						
							| 
									
										
										
										
											2002-02-18 12:39:32 +00:00
										 |  |  | #include <ctime>
 | 
					
						
							| 
									
										
										
										
											2002-02-16 16:09:08 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-07-12 10:41:24 +00:00
										 |  |  | #include <boost/detail/lightweight_thread.hpp>
 | 
					
						
							| 
									
										
										
										
											2002-02-16 16:09:08 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-07-12 10:41:24 +00:00
										 |  |  | //
 | 
					
						
							| 
									
										
										
										
											2002-02-16 16:09:08 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-07-12 10:41:24 +00:00
										 |  |  | int const n = 1024 * 1024; | 
					
						
							| 
									
										
										
										
											2002-02-16 16:09:08 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-07-12 10:41:24 +00:00
										 |  |  | void test( boost::shared_ptr<int> const & pi ) | 
					
						
							| 
									
										
										
										
											2002-02-16 16:09:08 +00:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2008-07-12 10:41:24 +00:00
										 |  |  |     std::vector< boost::shared_ptr<int> > v; | 
					
						
							| 
									
										
										
										
											2002-02-16 16:09:08 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-07-12 10:41:24 +00:00
										 |  |  |     for( int i = 0; i < n; ++i ) | 
					
						
							| 
									
										
										
										
											2002-02-16 16:09:08 +00:00
										 |  |  |     { | 
					
						
							| 
									
										
										
										
											2008-07-12 10:41:24 +00:00
										 |  |  |         v.push_back( pi ); | 
					
						
							| 
									
										
										
										
											2002-02-16 16:09:08 +00:00
										 |  |  |     } | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-07-12 10:41:24 +00:00
										 |  |  | int const m = 16; // threads
 | 
					
						
							| 
									
										
										
										
											2002-02-16 16:09:08 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-07-12 10:41:24 +00:00
										 |  |  | #if defined( BOOST_HAS_PTHREADS )
 | 
					
						
							| 
									
										
										
										
											2002-02-16 16:09:08 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-07-12 10:41:24 +00:00
										 |  |  | char const * thmodel = "POSIX"; | 
					
						
							| 
									
										
										
										
											2002-02-16 16:09:08 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-07-12 10:41:24 +00:00
										 |  |  | #else
 | 
					
						
							| 
									
										
										
										
											2002-02-16 16:09:08 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-07-12 10:41:24 +00:00
										 |  |  | char const * thmodel = "Windows"; | 
					
						
							| 
									
										
										
										
											2002-02-16 16:09:08 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | #endif
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2002-08-16 16:41:16 +00:00
										 |  |  | int main() | 
					
						
							| 
									
										
										
										
											2002-02-16 16:09:08 +00:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2003-02-08 16:05:46 +00:00
										 |  |  |     using namespace std; // printf, clock_t, clock
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-07-12 10:41:24 +00:00
										 |  |  |     printf( "Using %s threads: %d threads, %d iterations: ", thmodel, m, n ); | 
					
						
							| 
									
										
										
										
											2002-02-16 16:09:08 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-07-12 10:41:24 +00:00
										 |  |  |     boost::shared_ptr<int> pi( new int(42) ); | 
					
						
							| 
									
										
										
										
											2002-02-16 16:09:08 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2003-02-08 16:05:46 +00:00
										 |  |  |     clock_t t = clock(); | 
					
						
							| 
									
										
										
										
											2002-02-18 12:39:32 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-07-12 10:41:24 +00:00
										 |  |  |     pthread_t a[ m ]; | 
					
						
							| 
									
										
										
										
											2002-02-16 16:09:08 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-07-12 10:41:24 +00:00
										 |  |  |     for( int i = 0; i < m; ++i ) | 
					
						
							| 
									
										
										
										
											2002-02-16 16:09:08 +00:00
										 |  |  |     { | 
					
						
							| 
									
										
										
										
											2008-07-12 10:41:24 +00:00
										 |  |  |         boost::detail::lw_thread_create( a[ i ], boost::bind( test, pi ) ); | 
					
						
							| 
									
										
										
										
											2002-02-16 16:09:08 +00:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-07-12 10:41:24 +00:00
										 |  |  |     for( int j = 0; j < m; ++j ) | 
					
						
							| 
									
										
										
										
											2002-02-16 16:09:08 +00:00
										 |  |  |     { | 
					
						
							| 
									
										
										
										
											2008-07-12 10:41:24 +00:00
										 |  |  |         pthread_join( a[j], 0 ); | 
					
						
							| 
									
										
										
										
											2002-02-16 16:09:08 +00:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2002-02-16 18:45:20 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2003-02-08 16:05:46 +00:00
										 |  |  |     t = clock() - t; | 
					
						
							| 
									
										
										
										
											2002-02-18 12:39:32 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-07-12 10:41:24 +00:00
										 |  |  |     printf( "\n\n%.3f seconds.\n", static_cast<double>(t) / CLOCKS_PER_SEC ); | 
					
						
							| 
									
										
										
										
											2002-02-18 12:39:32 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2002-02-16 18:45:20 +00:00
										 |  |  |     return 0; | 
					
						
							| 
									
										
										
										
											2002-02-16 16:09:08 +00:00
										 |  |  | } |