]>
git.proxmox.com Git - ceph.git/blob - ceph/src/boost/libs/smart_ptr/test/weak_ptr_mt_test.cpp
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)
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>
11 static boost::shared_ptr
<int> sp( new int );
12 static boost::weak_ptr
<int> wp( sp
);
16 for( int i
= 0; i
< n
; ++i
)
18 boost::weak_ptr
<int> p1( wp
);
20 BOOST_TEST( !wp
.expired() );
21 BOOST_TEST( wp
.lock() != 0 );
27 for( int i
= 0; i
< n
; ++i
)
29 boost::weak_ptr
<int> p1( wp
);
31 BOOST_TEST( wp
.expired() );
32 BOOST_TEST( wp
.lock() == 0 );
38 int const N
= 100000; // iterations
39 int const M
= 8; // threads
41 boost::detail::lw_thread_t th
[ M
] = {};
43 for( int i
= 0; i
< M
; ++i
)
45 boost::detail::lw_thread_create( th
[ i
], boost::bind( f1
, N
) );
48 for( int i
= 0; i
< M
; ++i
)
50 boost::detail::lw_thread_join( th
[ i
] );
53 BOOST_TEST_EQ( sp
.use_count(), 1 );
54 BOOST_TEST_EQ( wp
.use_count(), 1 );
58 BOOST_TEST_EQ( sp
.use_count(), 0 );
59 BOOST_TEST_EQ( wp
.use_count(), 0 );
61 for( int i
= 0; i
< M
; ++i
)
63 boost::detail::lw_thread_create( th
[ i
], boost::bind( f2
, N
) );
66 for( int i
= 0; i
< M
; ++i
)
68 boost::detail::lw_thread_join( th
[ i
] );
73 return boost::report_errors();