test_threading.cpp 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. /* Copyright (C) 2011 John Maddock
  2. *
  3. * Use, modification and distribution is subject to the
  4. * Boost Software License, Version 1.0. (See accompanying
  5. * file LICENSE_1_0.txt or http://www.boost.org/LICENSE_1_0.txt)
  6. */
  7. #include <iostream>
  8. #include <boost/pool/pool_alloc.hpp>
  9. #include <boost/thread.hpp>
  10. #if defined(BOOST_MSVC) && (BOOST_MSVC <= 1600)
  11. #pragma warning(push)
  12. #pragma warning(disable: 4244)
  13. // ..\..\boost/random/uniform_int_distribution.hpp(171) :
  14. // warning C4127: conditional expression is constant
  15. #pragma warning(disable: 4127)
  16. // ..\..\boost/random/detail/polynomial.hpp(315) :
  17. // warning C4267: 'argument' : conversion from 'size_t'
  18. // to 'int', possible loss of data
  19. #pragma warning(disable: 4267)
  20. #endif
  21. #include <boost/random/mersenne_twister.hpp>
  22. #include <boost/random/uniform_int_distribution.hpp>
  23. #if defined(BOOST_MSVC) && (BOOST_MSVC <= 1600)
  24. #pragma warning(pop)
  25. #endif
  26. void run_tests()
  27. {
  28. boost::random::mt19937 gen;
  29. boost::random::uniform_int_distribution<> dist(-10, 10);
  30. std::list<int, boost::fast_pool_allocator<int> > l;
  31. for(int i = 0; i < 100; ++i)
  32. l.push_back(i);
  33. for(int i = 0; i < 20000; ++i)
  34. {
  35. int val = dist(gen);
  36. if(val < 0)
  37. {
  38. while(val && l.size())
  39. {
  40. l.pop_back();
  41. ++val;
  42. }
  43. }
  44. else
  45. {
  46. while(val)
  47. {
  48. l.push_back(val);
  49. --val;
  50. }
  51. }
  52. }
  53. }
  54. int main()
  55. {
  56. std::list<boost::shared_ptr<boost::thread> > threads;
  57. for(int i = 0; i < 10; ++i)
  58. {
  59. try{
  60. threads.push_back(boost::shared_ptr<boost::thread>(new boost::thread(&run_tests)));
  61. }
  62. catch(const std::exception& e)
  63. {
  64. std::cerr << "<note>Thread creation failed with message: " << e.what() << "</note>" << std::endl;
  65. }
  66. }
  67. std::list<boost::shared_ptr<boost::thread> >::const_iterator a(threads.begin()), b(threads.end());
  68. while(a != b)
  69. {
  70. (*a)->join();
  71. ++a;
  72. }
  73. return 0;
  74. }