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/smart_ptr/detail/spinlock_pool.hpp>
6#include <boost/smart_ptr/detail/lightweight_thread.hpp>
7#include <boost/bind/bind.hpp>
8#include <boost/core/lightweight_test.hpp>
9
10static int count = 0;
11
12void f( int n )
13{
14 for( int i = 0; i < n; ++i )
15 {
16 boost::detail::spinlock_pool<0>::scoped_lock lock( &count );
17 ++count;
18 }
19}
20
21int main()
22{
23 int const N = 100000; // iterations
24 int const M = 8; // threads
25
26 boost::detail::lw_thread_t th[ M ] = {};
27
28 for( int i = 0; i < M; ++i )
29 {
30 boost::detail::lw_thread_create( th&: th[ i ], f: boost::bind( f, a1: N ) );
31 }
32
33 for( int i = 0; i < M; ++i )
34 {
35 boost::detail::lw_thread_join( th: th[ i ] );
36 }
37
38 BOOST_TEST_EQ( count, N * M );
39
40 return boost::report_errors();
41}
42

source code of boost/libs/smart_ptr/test/spinlock_pool_mt_test.cpp