weak_ptr_mt_test.cpp 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  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. // weak_ptr_mt_test.cpp
  9. //
  10. // Copyright (c) 2002 Peter Dimov and Multi Media Ltd.
  11. // Copyright 2005, 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/weak_ptr.hpp>
  18. #include <boost/bind.hpp>
  19. #include <vector>
  20. #include <cstdio>
  21. #include <ctime>
  22. #include <cstdlib>
  23. #include <boost/detail/lightweight_thread.hpp>
  24. //
  25. int const n = 16384;
  26. int const k = 512; // vector size
  27. int const m = 16; // threads
  28. void test( std::vector< boost::shared_ptr<int> > & v )
  29. {
  30. using namespace std; // printf, rand
  31. std::vector< boost::weak_ptr<int> > w( v.begin(), v.end() );
  32. int s = 0, f = 0, r = 0;
  33. for( int i = 0; i < n; ++i )
  34. {
  35. // randomly kill a pointer
  36. v[ rand() % k ].reset();
  37. ++s;
  38. for( int j = 0; j < k; ++j )
  39. {
  40. if( boost::shared_ptr<int> px = w[ j ].lock() )
  41. {
  42. ++s;
  43. if( rand() & 4 )
  44. {
  45. continue;
  46. }
  47. // rebind anyway with prob. 50% for add_ref_lock() against weak_release() contention
  48. ++f;
  49. }
  50. else
  51. {
  52. ++r;
  53. }
  54. w[ j ] = v[ rand() % k ];
  55. }
  56. }
  57. printf( "\n%d locks, %d forced rebinds, %d normal rebinds.", s, f, r );
  58. }
  59. #if defined( BOOST_HAS_PTHREADS )
  60. char const * thmodel = "POSIX";
  61. #else
  62. char const * thmodel = "Windows";
  63. #endif
  64. int main()
  65. {
  66. using namespace std; // printf, clock_t, clock
  67. printf("Using %s threads: %d threads, %d * %d iterations: ", thmodel, m, n, k );
  68. std::vector< boost::shared_ptr<int> > v( k );
  69. for( int i = 0; i < k; ++i )
  70. {
  71. v[ i ].reset( new int( 0 ) );
  72. }
  73. clock_t t = clock();
  74. boost::detail::lw_thread_t a[ m ];
  75. for( int i = 0; i < m; ++i )
  76. {
  77. boost::detail::lw_thread_create( a[ i ], boost::bind( test, v ) );
  78. }
  79. v.resize( 0 ); // kill original copies
  80. for( int j = 0; j < m; ++j )
  81. {
  82. boost::detail::lw_thread_join( a[j] );
  83. }
  84. t = clock() - t;
  85. printf("\n\n%.3f seconds.\n", static_cast<double>(t) / CLOCKS_PER_SEC);
  86. return 0;
  87. }