1 | // Copyright 2018, 2020 Peter Dimov |
---|---|
2 | // Distributed under the Boost Software License, Version 1.0. |
3 | // https://www.boost.org/LICENSE_1_0.txt) |
4 | |
5 | #include <boost/shared_ptr.hpp> |
6 | #include <boost/weak_ptr.hpp> |
7 | #include <boost/smart_ptr/detail/lightweight_thread.hpp> |
8 | #include <boost/bind/bind.hpp> |
9 | #include <boost/core/lightweight_test.hpp> |
10 | |
11 | static boost::shared_ptr<int> sp( new int ); |
12 | |
13 | void f( int n ) |
14 | { |
15 | for( int i = 0; i < n; ++i ) |
16 | { |
17 | boost::shared_ptr<int> p1( sp ); |
18 | boost::weak_ptr<int> p2( p1 ); |
19 | } |
20 | } |
21 | |
22 | int main() |
23 | { |
24 | int const N = 100000; // iterations |
25 | int const M = 8; // threads |
26 | |
27 | boost::detail::lw_thread_t th[ M ] = {}; |
28 | |
29 | for( int i = 0; i < M; ++i ) |
30 | { |
31 | boost::detail::lw_thread_create( th&: th[ i ], f: boost::bind( f, a1: N ) ); |
32 | } |
33 | |
34 | for( int i = 0; i < M; ++i ) |
35 | { |
36 | boost::detail::lw_thread_join( th: th[ i ] ); |
37 | } |
38 | |
39 | BOOST_TEST_EQ( sp.use_count(), 1 ); |
40 | |
41 | sp.reset(); |
42 | |
43 | BOOST_TEST_EQ( sp.use_count(), 0 ); |
44 | |
45 | return boost::report_errors(); |
46 | } |
47 |