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 | |
6 | #define BOOST_THREAD_USES_MOVE |
7 | |
8 | #include <boost/config.hpp> |
9 | #include <boost/thread/thread.hpp> |
10 | #include <boost/thread/mutex.hpp> |
11 | #include <boost/thread/csbl/list.hpp> |
12 | //#include <boost/interprocess/smart_ptr/shared_ptr.hpp> |
13 | #include <boost/smart_ptr.hpp> |
14 | #include <iostream> |
15 | #include <boost/detail/lightweight_test.hpp> |
16 | |
17 | |
18 | int count = 0; |
19 | boost::mutex mutex; |
20 | |
21 | namespace { |
22 | |
23 | |
24 | template <typename TC> |
25 | void join_all(TC & tc) |
26 | { |
27 | for (typename TC::iterator it = tc.begin(); it != tc.end(); ++it) |
28 | { |
29 | (*it)->join(); |
30 | } |
31 | } |
32 | |
33 | |
34 | void increment_count() |
35 | { |
36 | boost::unique_lock<boost::mutex> lock(mutex); |
37 | std::cout << "count = " << ++count << std::endl; |
38 | } |
39 | |
40 | template <class T> |
41 | struct default_delete |
42 | { |
43 | typedef T* pointer; |
44 | |
45 | BOOST_CONSTEXPR default_delete() BOOST_NOEXCEPT {} //= default; |
46 | template <class U> |
47 | default_delete(const default_delete<U>&) BOOST_NOEXCEPT |
48 | {} |
49 | void operator()(T* ptr) const |
50 | { |
51 | delete ptr; |
52 | } |
53 | }; |
54 | |
55 | } |
56 | int main() |
57 | { |
58 | { |
59 | typedef boost::shared_ptr<boost::thread > thread_ptr; |
60 | //typedef boost::interprocess::shared_ptr<boost::thread, std::allocator<boost::thread>, default_delete<boost::thread> > thread_ptr; |
61 | typedef boost::csbl::list<thread_ptr > thread_ptr_list; |
62 | thread_ptr_list threads; |
63 | for (int i = 0; i < 10; ++i) |
64 | { |
65 | //threads.push_back(BOOST_THREAD_MAKE_RV_REF(thread_ptr(new boost::thread(&increment_count)))); |
66 | threads.push_back(x: thread_ptr(new boost::thread(&increment_count))); |
67 | } |
68 | BOOST_TEST(threads.size()==10); |
69 | //join_all(threads); |
70 | for (thread_ptr_list::iterator it = threads.begin(); it != threads.end(); ++it) |
71 | { |
72 | (*it)->join(); |
73 | } |
74 | } |
75 | count = 0; |
76 | { |
77 | typedef boost::shared_ptr<boost::thread > thread_ptr; |
78 | //typedef boost::interprocess::shared_ptr<boost::thread, std::allocator<boost::thread>, default_delete<boost::thread> > thread_ptr; |
79 | typedef boost::csbl::list<thread_ptr > thread_ptr_list; |
80 | thread_ptr_list threads; |
81 | for (int i = 0; i < 10; ++i) |
82 | { |
83 | //threads.push_back(BOOST_THREAD_MAKE_RV_REF(thread_ptr(new boost::thread(&increment_count)))); |
84 | threads.push_back(x: thread_ptr(new boost::thread(&increment_count))); |
85 | } |
86 | BOOST_TEST(threads.size()==10); |
87 | thread_ptr sth(new boost::thread(&increment_count)); |
88 | threads.push_back(x: sth); |
89 | BOOST_TEST(threads.size()==11); |
90 | threads.remove(value: sth); |
91 | BOOST_TEST(threads.size()==10); |
92 | sth->join(); |
93 | //join_all(threads); |
94 | for (thread_ptr_list::iterator it = threads.begin(); it != threads.end(); ++it) |
95 | { |
96 | (*it)->join(); |
97 | } |
98 | } |
99 | |
100 | return boost::report_errors(); |
101 | } |
102 | |