shared_ptr_mt_test.cpp 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. #include <boost/config.hpp>
  2. #if defined(BOOST_MSVC)
  3. #pragma warning(disable: 4786) // identifier truncated in debug info
  4. #pragma warning(disable: 4710) // function not inlined
  5. #pragma warning(disable: 4711) // function selected for automatic inline expansion
  6. #pragma warning(disable: 4514) // unreferenced inline removed
  7. #endif
  8. // shared_ptr_mt_test.cpp - tests shared_ptr with multiple threads
  9. //
  10. // Copyright (c) 2002 Peter Dimov and Multi Media Ltd.
  11. // Copyright (c) 2008 Peter Dimov
  12. //
  13. // Distributed under the Boost Software License, Version 1.0.
  14. // See accompanying file LICENSE_1_0.txt or copy at
  15. // http://www.boost.org/LICENSE_1_0.txt
  16. #include <boost/shared_ptr.hpp>
  17. #include <boost/bind.hpp>
  18. #include <vector>
  19. #include <cstdio>
  20. #include <ctime>
  21. #include <boost/detail/lightweight_thread.hpp>
  22. //
  23. int const n = 1024 * 1024;
  24. void test( boost::shared_ptr<int> const & pi )
  25. {
  26. std::vector< boost::shared_ptr<int> > v;
  27. for( int i = 0; i < n; ++i )
  28. {
  29. v.push_back( pi );
  30. }
  31. }
  32. int const m = 16; // threads
  33. #if defined( BOOST_HAS_PTHREADS )
  34. char const * thmodel = "POSIX";
  35. #else
  36. char const * thmodel = "Windows";
  37. #endif
  38. int main()
  39. {
  40. using namespace std; // printf, clock_t, clock
  41. printf( "Using %s threads: %d threads, %d iterations: ", thmodel, m, n );
  42. boost::shared_ptr<int> pi( new int(42) );
  43. clock_t t = clock();
  44. boost::detail::lw_thread_t a[ m ];
  45. for( int i = 0; i < m; ++i )
  46. {
  47. boost::detail::lw_thread_create( a[ i ], boost::bind( test, pi ) );
  48. }
  49. for( int j = 0; j < m; ++j )
  50. {
  51. boost::detail::lw_thread_join( a[j] );
  52. }
  53. t = clock() - t;
  54. printf( "\n\n%.3f seconds.\n", static_cast<double>(t) / CLOCKS_PER_SEC );
  55. return 0;
  56. }