1// Copyright 2023 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/unordered/detail/foa/rw_spinlock.hpp>
6#include <boost/core/lightweight_test.hpp>
7#include <mutex>
8#include <thread>
9#include <cstdio>
10
11using boost::unordered::detail::foa::rw_spinlock;
12
13static int count = 0;
14static rw_spinlock sp;
15
16void f( int k, int n )
17{
18 std::printf( format: "Thread %d started.\n", k );
19
20 for( int i = 0; i < n; ++i )
21 {
22 std::lock_guard<rw_spinlock> lock( sp );
23 ++count;
24 }
25
26 std::printf( format: "Thread %d finished.\n", k );
27}
28
29int main()
30{
31 int const N = 1000000; // iterations
32 int const M = 8; // threads
33
34 std::thread th[ M ];
35
36 for( int i = 0; i < M; ++i )
37 {
38 th[ i ] = std::thread( f, i, N );
39 }
40
41 for( int i = 0; i < M; ++i )
42 {
43 th[ i ].join();
44 }
45
46 BOOST_TEST_EQ( count, N * M );
47
48 return boost::report_errors();
49}
50

source code of boost/libs/unordered/test/cfoa/rw_spinlock_test3.cpp