forked from boostorg/smart_ptr
Add multithreaded tests
This commit is contained in:
16
test/Jamfile
16
test/Jamfile
@ -381,3 +381,19 @@ run ip_hash_test2.cpp ;
|
||||
run sp_hash_test4.cpp ;
|
||||
run lsp_hash_test.cpp ;
|
||||
run lsp_hash_test2.cpp ;
|
||||
|
||||
run atomic_count_mt_test.cpp
|
||||
: : : <threading>multi ;
|
||||
|
||||
run spinlock_mt_test.cpp
|
||||
: : : <threading>multi ;
|
||||
|
||||
run spinlock_pool_mt_test.cpp
|
||||
: : : <threading>multi ;
|
||||
|
||||
run shared_ptr_mt_test.cpp
|
||||
: : : <threading>multi ;
|
||||
|
||||
run weak_ptr_mt_test.cpp
|
||||
: : : <threading>multi ;
|
||||
|
||||
|
40
test/atomic_count_mt_test.cpp
Normal file
40
test/atomic_count_mt_test.cpp
Normal file
@ -0,0 +1,40 @@
|
||||
// Copyright 2018, 2020 Peter Dimov
|
||||
// Distributed under the Boost Software License, Version 1.0.
|
||||
// https://www.boost.org/LICENSE_1_0.txt)
|
||||
|
||||
#include <boost/smart_ptr/detail/atomic_count.hpp>
|
||||
#include <boost/smart_ptr/detail/lightweight_thread.hpp>
|
||||
#include <boost/bind/bind.hpp>
|
||||
#include <boost/core/lightweight_test.hpp>
|
||||
|
||||
static boost::detail::atomic_count count( 0 );
|
||||
|
||||
void f( int n )
|
||||
{
|
||||
for( int i = 0; i < n; ++i )
|
||||
{
|
||||
++count;
|
||||
}
|
||||
}
|
||||
|
||||
int main()
|
||||
{
|
||||
int const N = 100000; // iterations
|
||||
int const M = 8; // threads
|
||||
|
||||
boost::detail::lw_thread_t th[ M ] = {};
|
||||
|
||||
for( int i = 0; i < M; ++i )
|
||||
{
|
||||
boost::detail::lw_thread_create( th[ i ], boost::bind( f, N ) );
|
||||
}
|
||||
|
||||
for( int i = 0; i < M; ++i )
|
||||
{
|
||||
boost::detail::lw_thread_join( th[ i ] );
|
||||
}
|
||||
|
||||
BOOST_TEST_EQ( count, N * M );
|
||||
|
||||
return boost::report_errors();
|
||||
}
|
46
test/shared_ptr_mt_test.cpp
Normal file
46
test/shared_ptr_mt_test.cpp
Normal file
@ -0,0 +1,46 @@
|
||||
// Copyright 2018, 2020 Peter Dimov
|
||||
// Distributed under the Boost Software License, Version 1.0.
|
||||
// https://www.boost.org/LICENSE_1_0.txt)
|
||||
|
||||
#include <boost/shared_ptr.hpp>
|
||||
#include <boost/weak_ptr.hpp>
|
||||
#include <boost/smart_ptr/detail/lightweight_thread.hpp>
|
||||
#include <boost/bind/bind.hpp>
|
||||
#include <boost/core/lightweight_test.hpp>
|
||||
|
||||
static boost::shared_ptr<int> sp( new int );
|
||||
|
||||
void f( int n )
|
||||
{
|
||||
for( int i = 0; i < n; ++i )
|
||||
{
|
||||
boost::shared_ptr<int> p1( sp );
|
||||
boost::weak_ptr<int> p2( p1 );
|
||||
}
|
||||
}
|
||||
|
||||
int main()
|
||||
{
|
||||
int const N = 100000; // iterations
|
||||
int const M = 8; // threads
|
||||
|
||||
boost::detail::lw_thread_t th[ M ] = {};
|
||||
|
||||
for( int i = 0; i < M; ++i )
|
||||
{
|
||||
boost::detail::lw_thread_create( th[ i ], boost::bind( f, N ) );
|
||||
}
|
||||
|
||||
for( int i = 0; i < M; ++i )
|
||||
{
|
||||
boost::detail::lw_thread_join( th[ i ] );
|
||||
}
|
||||
|
||||
BOOST_TEST_EQ( sp.use_count(), 1 );
|
||||
|
||||
sp.reset();
|
||||
|
||||
BOOST_TEST_EQ( sp.use_count(), 0 );
|
||||
|
||||
return boost::report_errors();
|
||||
}
|
42
test/spinlock_mt_test.cpp
Normal file
42
test/spinlock_mt_test.cpp
Normal file
@ -0,0 +1,42 @@
|
||||
// Copyright 2018, 2020 Peter Dimov
|
||||
// Distributed under the Boost Software License, Version 1.0.
|
||||
// https://www.boost.org/LICENSE_1_0.txt)
|
||||
|
||||
#include <boost/smart_ptr/detail/spinlock.hpp>
|
||||
#include <boost/smart_ptr/detail/lightweight_thread.hpp>
|
||||
#include <boost/bind/bind.hpp>
|
||||
#include <boost/core/lightweight_test.hpp>
|
||||
|
||||
static int count = 0;
|
||||
static boost::detail::spinlock sp = BOOST_DETAIL_SPINLOCK_INIT;
|
||||
|
||||
void f( int n )
|
||||
{
|
||||
for( int i = 0; i < n; ++i )
|
||||
{
|
||||
boost::detail::spinlock::scoped_lock lock( sp );
|
||||
++count;
|
||||
}
|
||||
}
|
||||
|
||||
int main()
|
||||
{
|
||||
int const N = 100000; // iterations
|
||||
int const M = 8; // threads
|
||||
|
||||
boost::detail::lw_thread_t th[ M ] = {};
|
||||
|
||||
for( int i = 0; i < M; ++i )
|
||||
{
|
||||
boost::detail::lw_thread_create( th[ i ], boost::bind( f, N ) );
|
||||
}
|
||||
|
||||
for( int i = 0; i < M; ++i )
|
||||
{
|
||||
boost::detail::lw_thread_join( th[ i ] );
|
||||
}
|
||||
|
||||
BOOST_TEST_EQ( count, N * M );
|
||||
|
||||
return boost::report_errors();
|
||||
}
|
41
test/spinlock_pool_mt_test.cpp
Normal file
41
test/spinlock_pool_mt_test.cpp
Normal file
@ -0,0 +1,41 @@
|
||||
// Copyright 2018, 2020 Peter Dimov
|
||||
// Distributed under the Boost Software License, Version 1.0.
|
||||
// https://www.boost.org/LICENSE_1_0.txt)
|
||||
|
||||
#include <boost/smart_ptr/detail/spinlock_pool.hpp>
|
||||
#include <boost/smart_ptr/detail/lightweight_thread.hpp>
|
||||
#include <boost/bind/bind.hpp>
|
||||
#include <boost/core/lightweight_test.hpp>
|
||||
|
||||
static int count = 0;
|
||||
|
||||
void f( int n )
|
||||
{
|
||||
for( int i = 0; i < n; ++i )
|
||||
{
|
||||
boost::detail::spinlock_pool<0>::scoped_lock lock( &count );
|
||||
++count;
|
||||
}
|
||||
}
|
||||
|
||||
int main()
|
||||
{
|
||||
int const N = 100000; // iterations
|
||||
int const M = 8; // threads
|
||||
|
||||
boost::detail::lw_thread_t th[ M ] = {};
|
||||
|
||||
for( int i = 0; i < M; ++i )
|
||||
{
|
||||
boost::detail::lw_thread_create( th[ i ], boost::bind( f, N ) );
|
||||
}
|
||||
|
||||
for( int i = 0; i < M; ++i )
|
||||
{
|
||||
boost::detail::lw_thread_join( th[ i ] );
|
||||
}
|
||||
|
||||
BOOST_TEST_EQ( count, N * M );
|
||||
|
||||
return boost::report_errors();
|
||||
}
|
74
test/weak_ptr_mt_test.cpp
Normal file
74
test/weak_ptr_mt_test.cpp
Normal file
@ -0,0 +1,74 @@
|
||||
// Copyright 2018, 2020 Peter Dimov
|
||||
// Distributed under the Boost Software License, Version 1.0.
|
||||
// https://www.boost.org/LICENSE_1_0.txt)
|
||||
|
||||
#include <boost/shared_ptr.hpp>
|
||||
#include <boost/weak_ptr.hpp>
|
||||
#include <boost/smart_ptr/detail/lightweight_thread.hpp>
|
||||
#include <boost/bind/bind.hpp>
|
||||
#include <boost/core/lightweight_test.hpp>
|
||||
|
||||
static boost::shared_ptr<int> sp( new int );
|
||||
static boost::weak_ptr<int> wp( sp );
|
||||
|
||||
void f1( int n )
|
||||
{
|
||||
for( int i = 0; i < n; ++i )
|
||||
{
|
||||
boost::weak_ptr<int> p1( wp );
|
||||
|
||||
BOOST_TEST( !wp.expired() );
|
||||
BOOST_TEST( wp.lock() != 0 );
|
||||
}
|
||||
}
|
||||
|
||||
void f2( int n )
|
||||
{
|
||||
for( int i = 0; i < n; ++i )
|
||||
{
|
||||
boost::weak_ptr<int> p1( wp );
|
||||
|
||||
BOOST_TEST( wp.expired() );
|
||||
BOOST_TEST( wp.lock() == 0 );
|
||||
}
|
||||
}
|
||||
|
||||
int main()
|
||||
{
|
||||
int const N = 100000; // iterations
|
||||
int const M = 8; // threads
|
||||
|
||||
boost::detail::lw_thread_t th[ M ] = {};
|
||||
|
||||
for( int i = 0; i < M; ++i )
|
||||
{
|
||||
boost::detail::lw_thread_create( th[ i ], boost::bind( f1, N ) );
|
||||
}
|
||||
|
||||
for( int i = 0; i < M; ++i )
|
||||
{
|
||||
boost::detail::lw_thread_join( th[ i ] );
|
||||
}
|
||||
|
||||
BOOST_TEST_EQ( sp.use_count(), 1 );
|
||||
BOOST_TEST_EQ( wp.use_count(), 1 );
|
||||
|
||||
sp.reset();
|
||||
|
||||
BOOST_TEST_EQ( sp.use_count(), 0 );
|
||||
BOOST_TEST_EQ( wp.use_count(), 0 );
|
||||
|
||||
for( int i = 0; i < M; ++i )
|
||||
{
|
||||
boost::detail::lw_thread_create( th[ i ], boost::bind( f2, N ) );
|
||||
}
|
||||
|
||||
for( int i = 0; i < M; ++i )
|
||||
{
|
||||
boost::detail::lw_thread_join( th[ i ] );
|
||||
}
|
||||
|
||||
wp.reset();
|
||||
|
||||
return boost::report_errors();
|
||||
}
|
Reference in New Issue
Block a user