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.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;
11static boost::detail::spinlock sp = BOOST_DETAIL_SPINLOCK_INIT;
12
13void f( int n )
14{
15 for( int i = 0; i < n; ++i )
16 {
17 boost::detail::spinlock::scoped_lock lock( sp );
18 ++count;
19 }
20}
21
22int 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( count, N * M );
40
41 return boost::report_errors();
42}
43

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