Files
smart_ptr/extras/test/shared_ptr_mt_test.cpp

74 lines
1.4 KiB
C++
Raw Normal View History

2002-02-16 16:09:08 +00:00
// shared_ptr_mt_test.cpp - tests shared_ptr with multiple threads
//
// Copyright (c) 2002 Peter Dimov and Multi Media Ltd.
// Copyright (c) 2008 Peter Dimov
2002-02-16 16:09:08 +00:00
//
// Distributed under the Boost Software License, Version 1.0.
// See accompanying file LICENSE_1_0.txt or copy at
// http://www.boost.org/LICENSE_1_0.txt
2002-02-16 16:09:08 +00:00
#include <boost/shared_ptr.hpp>
#include <boost/bind.hpp>
2024-09-24 18:25:13 +03:00
#include <boost/config.hpp>
2002-02-16 16:09:08 +00:00
2024-09-24 18:25:13 +03:00
#include <boost/smart_ptr/detail/lightweight_thread.hpp>
2002-02-16 16:09:08 +00:00
2024-09-24 18:25:13 +03:00
#include <vector>
2002-02-16 16:09:08 +00:00
#include <cstdio>
2002-02-18 12:39:32 +00:00
#include <ctime>
2002-02-16 16:09:08 +00:00
//
2002-02-16 16:09:08 +00:00
int const n = 1024 * 1024;
2002-02-16 16:09:08 +00:00
void test( boost::shared_ptr<int> const & pi )
2002-02-16 16:09:08 +00:00
{
std::vector< boost::shared_ptr<int> > v;
2002-02-16 16:09:08 +00:00
for( int i = 0; i < n; ++i )
2002-02-16 16:09:08 +00:00
{
v.push_back( pi );
2002-02-16 16:09:08 +00:00
}
}
int const m = 16; // threads
2002-02-16 16:09:08 +00:00
#if defined( BOOST_HAS_PTHREADS )
2002-02-16 16:09:08 +00:00
char const * thmodel = "POSIX";
2002-02-16 16:09:08 +00:00
#else
2002-02-16 16:09:08 +00:00
char const * thmodel = "Windows";
2002-02-16 16:09:08 +00:00
#endif
int main()
2002-02-16 16:09:08 +00:00
{
using namespace std; // printf, clock_t, clock
printf( "Using %s threads: %d threads, %d iterations: ", thmodel, m, n );
2002-02-16 16:09:08 +00:00
boost::shared_ptr<int> pi( new int(42) );
2002-02-16 16:09:08 +00:00
clock_t t = clock();
2002-02-18 12:39:32 +00:00
boost::detail::lw_thread_t a[ m ];
2002-02-16 16:09:08 +00:00
for( int i = 0; i < m; ++i )
2002-02-16 16:09:08 +00:00
{
boost::detail::lw_thread_create( a[ i ], boost::bind( test, pi ) );
2002-02-16 16:09:08 +00:00
}
for( int j = 0; j < m; ++j )
2002-02-16 16:09:08 +00:00
{
boost::detail::lw_thread_join( a[j] );
2002-02-16 16:09:08 +00:00
}
t = clock() - t;
2002-02-18 12:39:32 +00:00
printf( "\n\n%.3f seconds.\n", static_cast<double>(t) / CLOCKS_PER_SEC );
2002-02-18 12:39:32 +00:00
return 0;
2002-02-16 16:09:08 +00:00
}