thread_ptr_list_pass.cpp 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. // Copyright (C) 2011 Vicente J. Botet Escriba
  2. //
  3. // Distributed under the Boost Software License, Version 1.0. (See accompanying
  4. // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  5. #define BOOST_THREAD_USES_MOVE
  6. #include <boost/config.hpp>
  7. #include <boost/thread/thread.hpp>
  8. #include <boost/thread/mutex.hpp>
  9. #include <boost/thread/csbl/list.hpp>
  10. //#include <boost/interprocess/smart_ptr/shared_ptr.hpp>
  11. #include <boost/smart_ptr.hpp>
  12. #include <iostream>
  13. #include <boost/detail/lightweight_test.hpp>
  14. int count = 0;
  15. boost::mutex mutex;
  16. namespace {
  17. template <typename TC>
  18. void join_all(TC & tc)
  19. {
  20. for (typename TC::iterator it = tc.begin(); it != tc.end(); ++it)
  21. {
  22. (*it)->join();
  23. }
  24. }
  25. void increment_count()
  26. {
  27. boost::unique_lock<boost::mutex> lock(mutex);
  28. std::cout << "count = " << ++count << std::endl;
  29. }
  30. template <class T>
  31. struct default_delete
  32. {
  33. typedef T* pointer;
  34. BOOST_CONSTEXPR default_delete() BOOST_NOEXCEPT {} //= default;
  35. template <class U>
  36. default_delete(const default_delete<U>&) BOOST_NOEXCEPT
  37. {}
  38. void operator()(T* ptr) const
  39. {
  40. delete ptr;
  41. }
  42. };
  43. }
  44. int main()
  45. {
  46. {
  47. typedef boost::shared_ptr<boost::thread > thread_ptr;
  48. //typedef boost::interprocess::shared_ptr<boost::thread, std::allocator<boost::thread>, default_delete<boost::thread> > thread_ptr;
  49. typedef boost::csbl::list<thread_ptr > thread_ptr_list;
  50. thread_ptr_list threads;
  51. for (int i = 0; i < 10; ++i)
  52. {
  53. //threads.push_back(BOOST_THREAD_MAKE_RV_REF(thread_ptr(new boost::thread(&increment_count))));
  54. threads.push_back(thread_ptr(new boost::thread(&increment_count)));
  55. }
  56. BOOST_TEST(threads.size()==10);
  57. //join_all(threads);
  58. for (thread_ptr_list::iterator it = threads.begin(); it != threads.end(); ++it)
  59. {
  60. (*it)->join();
  61. }
  62. }
  63. count = 0;
  64. {
  65. typedef boost::shared_ptr<boost::thread > thread_ptr;
  66. //typedef boost::interprocess::shared_ptr<boost::thread, std::allocator<boost::thread>, default_delete<boost::thread> > thread_ptr;
  67. typedef boost::csbl::list<thread_ptr > thread_ptr_list;
  68. thread_ptr_list threads;
  69. for (int i = 0; i < 10; ++i)
  70. {
  71. //threads.push_back(BOOST_THREAD_MAKE_RV_REF(thread_ptr(new boost::thread(&increment_count))));
  72. threads.push_back(thread_ptr(new boost::thread(&increment_count)));
  73. }
  74. BOOST_TEST(threads.size()==10);
  75. thread_ptr sth(new boost::thread(&increment_count));
  76. threads.push_back(sth);
  77. BOOST_TEST(threads.size()==11);
  78. threads.remove(sth);
  79. BOOST_TEST(threads.size()==10);
  80. sth->join();
  81. //join_all(threads);
  82. for (thread_ptr_list::iterator it = threads.begin(); it != threads.end(); ++it)
  83. {
  84. (*it)->join();
  85. }
  86. }
  87. return boost::report_errors();
  88. }