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

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