Compare commits

...

6 Commits

Author SHA1 Message Date
Peter Dimov 9a7d1d336a Update ci.yml 2023-06-26 21:41:13 +03:00
Peter Dimov 3fc18d56ac Remove latch_tests.cpp 2023-06-26 16:13:20 +03:00
Peter Dimov 6bcf457b46 Use boost::compat::latch 2023-06-26 10:32:52 +03:00
Peter Dimov 01b2369700 Update .appveyor.yml 2023-06-26 02:45:47 +03:00
Peter Dimov fcb3f0e82a Decrease iterations in rw_spinlock_test8 to avoid timeouts 2023-06-26 02:42:12 +03:00
Peter Dimov 1e4deb10a1 Add rw_spinlock tests 2023-06-25 19:03:02 +03:00
21 changed files with 441 additions and 282 deletions
+30 -16
View File
@@ -31,14 +31,28 @@ environment:
B2_VARIANT: debug,release
matrix:
- FLAVOR: Visual Studio 2008, 2010, 2012
- FLAVOR: Visual Studio 2008
APPVEYOR_BUILD_WORKER_IMAGE: Visual Studio 2015
B2_TOOLSET: msvc-9.0,msvc-10.0,msvc-11.0
B2_TOOLSET: msvc-9.0
B2_ADDRESS_MODEL: 32 # No 64bit support
- FLAVOR: Visual Studio 2013, 2015
- FLAVOR: Visual Studio 2010
APPVEYOR_BUILD_WORKER_IMAGE: Visual Studio 2015
B2_TOOLSET: msvc-12.0,msvc-14.0
B2_TOOLSET: msvc-10.0
B2_ADDRESS_MODEL: 32 # No 64bit support
- FLAVOR: Visual Studio 2012
APPVEYOR_BUILD_WORKER_IMAGE: Visual Studio 2015
B2_TOOLSET: msvc-11.0
B2_ADDRESS_MODEL: 32 # No 64bit support
- FLAVOR: Visual Studio 2013
APPVEYOR_BUILD_WORKER_IMAGE: Visual Studio 2015
B2_TOOLSET: msvc-12.0
- FLAVOR: Visual Studio 2015
APPVEYOR_BUILD_WORKER_IMAGE: Visual Studio 2015
B2_TOOLSET: msvc-14.0
- FLAVOR: Visual Studio 2017, C++14
APPVEYOR_BUILD_WORKER_IMAGE: Visual Studio 2017
@@ -55,32 +69,32 @@ environment:
B2_CXXSTD: latest
B2_TOOLSET: msvc-14.1
- FLAVOR: cygwin (32-bit, C++03,11)
- FLAVOR: cygwin (32-bit, C++03)
APPVEYOR_BUILD_WORKER_IMAGE: Visual Studio 2017
ADDPATH: C:\cygwin\bin;
B2_ADDRESS_MODEL: 32
B2_CXXSTD: 03,11
B2_CXXSTD: 03
B2_TOOLSET: gcc
- FLAVOR: cygwin (32-bit, C++14,1z)
- FLAVOR: cygwin (32-bit, C++11)
APPVEYOR_BUILD_WORKER_IMAGE: Visual Studio 2017
ADDPATH: C:\cygwin\bin;
B2_ADDRESS_MODEL: 32
B2_CXXSTD: 14,1z
B2_CXXSTD: 11
B2_TOOLSET: gcc
- FLAVOR: cygwin (64-bit, C++03,11)
- FLAVOR: cygwin (32-bit, C++14)
APPVEYOR_BUILD_WORKER_IMAGE: Visual Studio 2017
ADDPATH: C:\cygwin64\bin;
B2_ADDRESS_MODEL: 64
B2_CXXSTD: 03,11
ADDPATH: C:\cygwin\bin;
B2_ADDRESS_MODEL: 32
B2_CXXSTD: 14
B2_TOOLSET: gcc
- FLAVOR: cygwin (64-bit, C++14,1z)
- FLAVOR: cygwin (32-bit, C++1z)
APPVEYOR_BUILD_WORKER_IMAGE: Visual Studio 2017
ADDPATH: C:\cygwin64\bin;
B2_ADDRESS_MODEL: 64
B2_CXXSTD: 14,1z
ADDPATH: C:\cygwin\bin;
B2_ADDRESS_MODEL: 32
B2_CXXSTD: 1z
B2_TOOLSET: gcc
- FLAVOR: cygwin (64-bit, latest, C++03)
+12 -10
View File
@@ -44,7 +44,7 @@ jobs:
# Linux, gcc
- { compiler: gcc-7, cxxstd: '03,11,14,17', os: ubuntu-20.04, install: 'g++-7' }
- { compiler: gcc-8, cxxstd: '03,11,14,17', os: ubuntu-20.04, install: 'g++-8' }
- { compiler: gcc-9, cxxstd: '03,11,14,17', os: ubuntu-20.04, install: 'g++-9' }
- { compiler: gcc-9, cxxstd: '03,11,14,17', os: ubuntu-22.04, install: 'g++-9' }
- { compiler: gcc-10, cxxstd: '03,11,14,17,20', os: ubuntu-22.04, install: 'g++-10' }
- { compiler: gcc-11, cxxstd: '03,11,14,17,20', os: ubuntu-22.04, install: 'g++-11' }
- { name: "gcc-12 w/ sanitizers (03,11,14)", sanitize: yes,
@@ -54,8 +54,8 @@ jobs:
- { name: Collect coverage, coverage: yes,
compiler: gcc-12, cxxstd: '03,20', os: ubuntu-22.04, install: 'g++-12-multilib', address-model: '32,64', ccache_key: "cov" }
- { name: "cfoa tsan (gcc)", cxxstd: '11,14,17,20,2b', os: ubuntu-22.04, compiler: gcc-12,
targets: 'libs/unordered/test//cfoa_tests', thread-sanitize: yes }
- { name: "cfoa tsan (gcc-12)", cxxstd: '11,14,17,20,2b', os: ubuntu-22.04, compiler: gcc-12,
targets: 'libs/unordered/test//cfoa_tests', thread-sanitize: yes, ccache_key: "tsan" }
# Linux, clang, libc++
- { compiler: clang-7, cxxstd: '03,11,14,17', os: ubuntu-20.04, stdlib: libc++, install: 'clang-7 libc++-7-dev libc++abi-7-dev' }
@@ -67,7 +67,6 @@ jobs:
- { name: "clang-12 w/ sanitizers (17,20,2b)", sanitize: yes,
compiler: clang-12, cxxstd: '17,20,2b', os: ubuntu-20.04, stdlib: libc++, install: 'clang-12 libc++-12-dev libc++abi-12-dev', ccache_key: "san2" }
- { compiler: clang-13, cxxstd: '03,11,14,17,20,2b', os: ubuntu-22.04, stdlib: libc++, install: 'clang-13 libc++-13-dev libc++abi-13-dev' }
- { compiler: clang-14, cxxstd: '03,11,14,17,20,2b', os: ubuntu-22.04, stdlib: libc++, install: 'clang-14 libc++-14-dev libc++abi-14-dev' }
# not using libc++ because of https://github.com/llvm/llvm-project/issues/52771
- { name: "clang-14 w/ sanitizers (03,11,14)", sanitize: yes,
@@ -75,16 +74,19 @@ jobs:
- { name: "clang-14 w/ sanitizers (17,20,2b)", sanitize: yes,
compiler: clang-14, cxxstd: '17,20,2b', os: ubuntu-22.04, ccache_key: "san2" }
- { name: "cfoa tsan (clang)", cxxstd: '11,14,17,20,2b', os: ubuntu-22.04, compiler: clang-14,
- { name: "cfoa tsan (clang-14)", cxxstd: '11,14,17,20,2b', os: ubuntu-22.04, compiler: clang-14,
targets: 'libs/unordered/test//cfoa_tests', thread-sanitize: yes,
stdlib: libc++, install: 'clang-14 libc++-14-dev libc++abi-14-dev' }
stdlib: libc++, install: 'clang-14 libc++-14-dev libc++abi-14-dev', ccache_key: "tsan" }
- { compiler: clang-15, cxxstd: '03,11,14,17,20,2b', os: ubuntu-22.04, stdlib: libc++, install: 'clang-15 libc++-15-dev libc++abi-15-dev' }
# OSX, clang
- { compiler: clang, cxxstd: '03,11,14,17,2a', os: macos-11, }
- { compiler: clang, cxxstd: '03,11,14,17,2a', os: macos-12, sanitize: yes }
- { compiler: clang, cxxstd: '11,14,17,2a', os: macos-12, thread-sanitize: yes, targets: 'libs/unordered/test//cfoa_tests' }
- { compiler: clang, cxxstd: '03,11,14,17,2a', os: macos-11 }
- { compiler: clang, cxxstd: '03,11,14,17,2a', os: macos-12, sanitize: yes, ccache_key: "san1" }
- { compiler: clang, cxxstd: '11,14,17,2a', os: macos-12, thread-sanitize: yes, targets: 'libs/unordered/test//cfoa_tests', ccache_key: "tsan" }
- { compiler: clang, cxxstd: '03,11,14,17,20,2b', os: macos-13 }
timeout-minutes: 180
timeout-minutes: 210
runs-on: ${{matrix.os}}
container: ${{matrix.container}}
env: {B2_USE_CCACHE: 1}
+9 -2
View File
@@ -20,7 +20,7 @@ function(foa_tests)
endfunction()
function(cfoa_tests)
boost_test(PREFIX boost_unordered_cfoa LINK_LIBRARIES Threads::Threads ${ARGN})
boost_test(PREFIX boost_unordered_cfoa LINK_LIBRARIES Boost::compat Threads::Threads ${ARGN})
endfunction()
# FCA tests
@@ -140,7 +140,6 @@ foa_tests(SOURCES exception/merge_exception_tests.cpp)
# CFOA tests
cfoa_tests(SOURCES cfoa/latch_tests.cpp)
cfoa_tests(SOURCES cfoa/insert_tests.cpp)
cfoa_tests(SOURCES cfoa/erase_tests.cpp)
cfoa_tests(SOURCES cfoa/try_emplace_tests.cpp)
@@ -159,5 +158,13 @@ cfoa_tests(SOURCES cfoa/exception_erase_tests.cpp)
cfoa_tests(SOURCES cfoa/exception_constructor_tests.cpp)
cfoa_tests(SOURCES cfoa/exception_assign_tests.cpp)
cfoa_tests(SOURCES cfoa/exception_merge_tests.cpp)
cfoa_tests(SOURCES cfoa/rw_spinlock_test.cpp)
cfoa_tests(SOURCES cfoa/rw_spinlock_test2.cpp)
cfoa_tests(SOURCES cfoa/rw_spinlock_test3.cpp)
cfoa_tests(SOURCES cfoa/rw_spinlock_test4.cpp)
cfoa_tests(SOURCES cfoa/rw_spinlock_test5.cpp)
cfoa_tests(SOURCES cfoa/rw_spinlock_test6.cpp)
cfoa_tests(SOURCES cfoa/rw_spinlock_test7.cpp)
cfoa_tests(SOURCES cfoa/rw_spinlock_test8.cpp)
endif()
+8 -1
View File
@@ -176,7 +176,6 @@ alias foa_tests :
;
local CFOA_TESTS =
latch_tests
insert_tests
erase_tests
try_emplace_tests
@@ -195,6 +194,14 @@ local CFOA_TESTS =
exception_constructor_tests
exception_assign_tests
exception_merge_tests
rw_spinlock_test
rw_spinlock_test2
rw_spinlock_test3
rw_spinlock_test4
rw_spinlock_test5
rw_spinlock_test6
rw_spinlock_test7
rw_spinlock_test8
;
for local test in $(CFOA_TESTS)
+1 -1
View File
@@ -787,7 +787,7 @@ namespace {
std::thread t1, t2, t3;
boost::latch start_latch(2), end_latch(2);
boost::compat::latch start_latch(2), end_latch(2);
auto v1 = make_random_values(1024 * 16, [&] { return gen(rg); });
auto v2 = v1;
+2 -3
View File
@@ -2,11 +2,10 @@
// 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)
#include "latch.hpp"
#include "../helpers/generators.hpp"
#include "../helpers/test.hpp"
#include <boost/compat/latch.hpp>
#include <boost/container_hash/hash.hpp>
#include <boost/core/span.hpp>
#include <boost/unordered/unordered_flat_map.hpp>
@@ -370,7 +369,7 @@ std::vector<boost::span<T> > split(
template <class T, class F> void thread_runner(std::vector<T>& values, F f)
{
boost::latch latch(static_cast<std::ptrdiff_t>(num_threads));
boost::compat::latch latch(static_cast<std::ptrdiff_t>(num_threads));
std::vector<std::thread> threads;
auto subslices = split<T>(values, num_threads);
+2 -3
View File
@@ -5,11 +5,10 @@
#ifndef BOOST_UNORDERED_TEST_CFOA_HELPERS_HPP
#define BOOST_UNORDERED_TEST_CFOA_HELPERS_HPP
#include "latch.hpp"
#include "../helpers/generators.hpp"
#include "../helpers/test.hpp"
#include <boost/compat/latch.hpp>
#include <boost/container_hash/hash.hpp>
#include <boost/core/span.hpp>
#include <boost/unordered/unordered_flat_map.hpp>
@@ -339,7 +338,7 @@ std::vector<boost::span<T> > split(
template <class T, class F> void thread_runner(std::vector<T>& values, F f)
{
boost::latch latch(static_cast<std::ptrdiff_t>(num_threads));
boost::compat::latch latch(static_cast<std::ptrdiff_t>(num_threads));
std::vector<std::thread> threads;
auto subslices = split<T>(values, num_threads);
-87
View File
@@ -1,87 +0,0 @@
// Copyright (C) 2023 Christian Mazakas
// 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)
#ifndef BOOST_UNORDERED_TEST_CFOA_LATCH_HPP
#define BOOST_UNORDERED_TEST_CFOA_LATCH_HPP
#include <boost/assert.hpp>
#include <climits>
#include <condition_variable>
#include <cstddef>
#include <mutex>
namespace boost {
class latch
{
private:
std::ptrdiff_t n_;
mutable std::mutex m_;
mutable std::condition_variable cv_;
public:
explicit latch(std::ptrdiff_t expected) : n_{expected}, m_{}, cv_{}
{
BOOST_ASSERT(n_ >= 0);
BOOST_ASSERT(n_ <= max());
}
latch(latch const&) = delete;
latch& operator=(latch const&) = delete;
~latch() = default;
void count_down(std::ptrdiff_t n = 1)
{
std::unique_lock<std::mutex> lk(m_);
count_down_and_notify(lk, n);
}
bool try_wait() const noexcept
{
std::unique_lock<std::mutex> lk(m_);
return is_ready();
}
void wait() const
{
std::unique_lock<std::mutex> lk(m_);
wait_impl(lk);
}
void arrive_and_wait(std::ptrdiff_t n = 1)
{
std::unique_lock<std::mutex> lk(m_);
bool should_wait = count_down_and_notify(lk, n);
if (should_wait) {
wait_impl(lk);
}
}
static constexpr std::ptrdiff_t max() noexcept { return INT_MAX; }
private:
bool is_ready() const { return n_ == 0; }
bool count_down_and_notify(
std::unique_lock<std::mutex>& lk, std::ptrdiff_t n)
{
n_ -= n;
if (n_ == 0) {
lk.unlock();
cv_.notify_all();
return false;
}
return true;
}
void wait_impl(std::unique_lock<std::mutex>& lk) const
{
cv_.wait(lk, [this] { return this->is_ready(); });
}
};
} // namespace boost
#endif // BOOST_UNORDERED_TEST_CFOA_LATCH_HPP
-155
View File
@@ -1,155 +0,0 @@
// Copyright (C) 2023 Christian Mazakas
// 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)
#define BOOST_ENABLE_ASSERT_HANDLER
#include "latch.hpp"
#include <boost/core/lightweight_test.hpp>
#include <thread>
#include <vector>
struct exception
{
};
namespace boost {
void assertion_failed(
char const* expr, char const* function, char const* file, long line)
{
(void)expr;
(void)function;
(void)file;
(void)line;
throw exception{};
}
} // namespace boost
namespace {
void test_max() { BOOST_TEST_EQ(boost::latch::max(), INT_MAX); }
void test_constructor()
{
{
auto const f = [] {
boost::latch l(-1);
(void)l;
};
BOOST_TEST_THROWS(f(), exception);
}
{
std::ptrdiff_t n = 0;
boost::latch l(n);
BOOST_TEST(l.try_wait());
}
{
std::ptrdiff_t n = 16;
boost::latch l(n);
BOOST_TEST_NOT(l.try_wait());
l.count_down(16);
BOOST_TEST(l.try_wait());
}
#if PTRDIFF_MAX > INT_MAX
{
auto const f = [] {
std::ptrdiff_t n = INT_MAX;
n += 10;
boost::latch l(n);
(void)l;
};
BOOST_TEST_THROWS(f(), exception);
}
#endif
}
void test_count_down_and_wait()
{
constexpr std::ptrdiff_t n = 1024;
boost::latch l(2 * n);
bool bs[] = {false, false};
std::thread t1([&] {
l.wait();
BOOST_TEST(bs[0]);
BOOST_TEST(bs[1]);
});
std::thread t2([&] {
for (int i = 0; i < n; ++i) {
if (i == (n - 1)) {
bs[0] = true;
} else {
BOOST_TEST_NOT(l.try_wait());
}
l.count_down(1);
}
});
for (int i = 0; i < n; ++i) {
if (i == (n - 1)) {
bs[1] = true;
} else {
BOOST_TEST_NOT(l.try_wait());
}
l.count_down(1);
}
t1.join();
t2.join();
BOOST_TEST(l.try_wait());
}
void test_arrive_and_wait()
{
std::ptrdiff_t const n = 16;
boost::latch l(2 * n);
int xs[n] = {0};
std::vector<std::thread> threads;
for (int i = 0; i < n; ++i) {
threads.emplace_back([&l, &xs, i, n] {
(void)n;
for (int j = 0; j < n; ++j) {
BOOST_TEST_EQ(xs[j], 0);
}
l.arrive_and_wait(2);
xs[i] = 1;
});
}
for (auto& t : threads) {
t.join();
}
for (int i = 0; i < n; ++i) {
BOOST_TEST_EQ(xs[i], 1);
}
}
} // namespace
int main()
{
test_max();
test_constructor();
test_count_down_and_wait();
test_arrive_and_wait();
return boost::report_errors();
}
+1 -1
View File
@@ -103,7 +103,7 @@ namespace {
map2_type x2(2 * vals1.size(), allocator_type(3));
std::thread t1, t2, t3;
boost::latch l(2);
boost::compat::latch l(2);
std::mutex m;
std::condition_variable cv;
+1 -1
View File
@@ -79,7 +79,7 @@ namespace {
map_type x(0, hasher(1), key_equal(2), allocator_type(3));
std::thread t1, t2, t3;
boost::latch l(2);
boost::compat::latch l(2);
std::mutex m;
std::condition_variable cv;
+26
View File
@@ -0,0 +1,26 @@
// Copyright 2023 Peter Dimov
// Distributed under the Boost Software License, Version 1.0.
// https://www.boost.org/LICENSE_1_0.txt
#include <boost/unordered/detail/foa/rw_spinlock.hpp>
#include <mutex>
using boost::unordered::detail::foa::rw_spinlock;
// Sanity check only
static rw_spinlock sp;
static rw_spinlock sp2;
int main()
{
sp.lock();
sp2.lock();
sp.unlock();
sp2.unlock();
{
std::lock_guard<rw_spinlock> lock( sp );
std::lock_guard<rw_spinlock> lock2( sp2 );
}
}
+41
View File
@@ -0,0 +1,41 @@
// Copyright 2023 Peter Dimov
// Distributed under the Boost Software License, Version 1.0.
// https://www.boost.org/LICENSE_1_0.txt
#include <boost/unordered/detail/foa/rw_spinlock.hpp>
#include <boost/core/lightweight_test.hpp>
#include <mutex>
using boost::unordered::detail::foa::rw_spinlock;
static rw_spinlock sp;
static rw_spinlock sp2;
int main()
{
BOOST_TEST( sp.try_lock() );
BOOST_TEST( !sp.try_lock() );
BOOST_TEST( sp2.try_lock() );
BOOST_TEST( !sp.try_lock() );
BOOST_TEST( !sp2.try_lock() );
sp.unlock();
sp2.unlock();
sp.lock();
BOOST_TEST( !sp.try_lock() );
sp2.lock();
BOOST_TEST( !sp.try_lock() );
BOOST_TEST( !sp2.try_lock() );
sp.unlock();
sp2.unlock();
{
std::lock_guard<rw_spinlock> lock( sp );
BOOST_TEST( !sp.try_lock() );
std::lock_guard<rw_spinlock> lock2( sp2 );
BOOST_TEST( !sp.try_lock() );
BOOST_TEST( !sp2.try_lock() );
}
return boost::report_errors();
}
+49
View File
@@ -0,0 +1,49 @@
// Copyright 2023 Peter Dimov
// Distributed under the Boost Software License, Version 1.0.
// https://www.boost.org/LICENSE_1_0.txt
#include <boost/unordered/detail/foa/rw_spinlock.hpp>
#include <boost/core/lightweight_test.hpp>
#include <mutex>
#include <thread>
#include <cstdio>
using boost::unordered::detail::foa::rw_spinlock;
static int count = 0;
static rw_spinlock sp;
void f( int k, int n )
{
std::printf( "Thread %d started.\n", k );
for( int i = 0; i < n; ++i )
{
std::lock_guard<rw_spinlock> lock( sp );
++count;
}
std::printf( "Thread %d finished.\n", k );
}
int main()
{
int const N = 1000000; // iterations
int const M = 8; // threads
std::thread th[ M ];
for( int i = 0; i < M; ++i )
{
th[ i ] = std::thread( f, i, N );
}
for( int i = 0; i < M; ++i )
{
th[ i ].join();
}
BOOST_TEST_EQ( count, N * M );
return boost::report_errors();
}
+29
View File
@@ -0,0 +1,29 @@
// Copyright 2023 Peter Dimov
// Distributed under the Boost Software License, Version 1.0.
// https://www.boost.org/LICENSE_1_0.txt
#include <boost/unordered/detail/foa/rw_spinlock.hpp>
#include <boost/compat/shared_lock.hpp>
#include <mutex>
using boost::unordered::detail::foa::rw_spinlock;
static rw_spinlock sp;
static rw_spinlock sp2;
int main()
{
sp.lock();
sp2.lock_shared();
sp2.lock_shared();
sp.unlock();
sp2.unlock_shared();
sp2.unlock_shared();
{
std::lock_guard<rw_spinlock> lock( sp );
boost::compat::shared_lock<rw_spinlock> lock2( sp2 );
boost::compat::shared_lock<rw_spinlock> lock3( sp2 );
}
}
+42
View File
@@ -0,0 +1,42 @@
// Copyright 2023 Peter Dimov
// Distributed under the Boost Software License, Version 1.0.
// https://www.boost.org/LICENSE_1_0.txt
#include <boost/unordered/detail/foa/rw_spinlock.hpp>
#include <boost/compat/shared_lock.hpp>
#include <boost/core/lightweight_test.hpp>
#include <mutex>
using boost::unordered::detail::foa::rw_spinlock;
static rw_spinlock sp;
int main()
{
{
BOOST_TEST( sp.try_lock_shared() );
BOOST_TEST( sp.try_lock_shared() );
sp.unlock_shared();
sp.unlock_shared();
}
{
BOOST_TEST( sp.try_lock() );
BOOST_TEST( !sp.try_lock_shared() );
sp.unlock();
}
{
std::lock_guard<rw_spinlock> lock( sp );
BOOST_TEST( !sp.try_lock_shared() );
}
{
boost::compat::shared_lock<rw_spinlock> lock( sp );
BOOST_TEST( !sp.try_lock() );
BOOST_TEST( sp.try_lock_shared() );
sp.unlock_shared();
}
return boost::report_errors();
}
+60
View File
@@ -0,0 +1,60 @@
// Copyright 2023 Peter Dimov
// Distributed under the Boost Software License, Version 1.0.
// https://www.boost.org/LICENSE_1_0.txt
#include <boost/unordered/detail/foa/rw_spinlock.hpp>
#include <boost/compat/shared_lock.hpp>
#include <boost/core/lightweight_test.hpp>
#include <mutex>
#include <thread>
#include <cstdio>
using boost::unordered::detail::foa::rw_spinlock;
static int count = 0;
static rw_spinlock sp;
void f( int k, int n )
{
std::printf( "Thread %d started.\n", k );
int i = 0;
for( ;; ++i )
{
{
boost::compat::shared_lock<rw_spinlock> lock( sp );
if( count >= n ) break;
}
{
std::lock_guard<rw_spinlock> lock( sp );
if( count >= n ) break;
++count;
}
}
std::printf( "Thread %d finished (%i iterations).\n", k, i );
}
int main()
{
int const N = 1000000; // total iterations
int const M = 8; // threads
std::thread th[ M ];
for( int i = 0; i < M; ++i )
{
th[ i ] = std::thread( f, i, N );
}
for( int i = 0; i < M; ++i )
{
th[ i ].join();
}
BOOST_TEST_EQ( count, N );
return boost::report_errors();
}
+62
View File
@@ -0,0 +1,62 @@
// Copyright 2023 Peter Dimov
// Distributed under the Boost Software License, Version 1.0.
// https://www.boost.org/LICENSE_1_0.txt
#include <boost/unordered/detail/foa/rw_spinlock.hpp>
#include <boost/compat/shared_lock.hpp>
#include <boost/core/lightweight_test.hpp>
#include <mutex>
#include <thread>
#include <cstdio>
using boost::unordered::detail::foa::rw_spinlock;
static int count = 0;
static rw_spinlock sp;
void f( int k, int n )
{
std::printf( "Thread %d started.\n", k );
int i = 0;
for( ;; ++i )
{
int oldc;
{
boost::compat::shared_lock<rw_spinlock> lock( sp );
if( count >= n ) break;
oldc = count;
}
{
std::lock_guard<rw_spinlock> lock( sp );
if( count == oldc ) ++count;
}
}
std::printf( "Thread %d finished (%i iterations).\n", k, i );
}
int main()
{
int const N = 1000000; // total iterations
int const M = 8; // threads
std::thread th[ M ];
for( int i = 0; i < M; ++i )
{
th[ i ] = std::thread( f, i, N );
}
for( int i = 0; i < M; ++i )
{
th[ i ].join();
}
BOOST_TEST_EQ( count, N );
return boost::report_errors();
}
+64
View File
@@ -0,0 +1,64 @@
// Copyright 2023 Peter Dimov
// Distributed under the Boost Software License, Version 1.0.
// https://www.boost.org/LICENSE_1_0.txt
#include <boost/unordered/detail/foa/rw_spinlock.hpp>
#include <boost/compat/shared_lock.hpp>
#include <boost/core/lightweight_test.hpp>
#include <mutex>
#include <thread>
#include <cstdio>
using boost::unordered::detail::foa::rw_spinlock;
static int count = 0;
static rw_spinlock sp;
void f( int k, int m, int n )
{
std::printf( "Thread %d of %d started.\n", k, m );
for( int i = 0; i < n; ++i )
{
int oldc;
for( ;; )
{
{
boost::compat::shared_lock<rw_spinlock> lock( sp );
oldc = count;
}
if( oldc % m == k ) break;
}
{
std::lock_guard<rw_spinlock> lock( sp );
if( count == oldc ) ++count;
}
}
std::printf( "Thread %d of %d finished.\n", k, m );
}
int main()
{
int const N = 100; // total iterations
int const M = 4; // threads
std::thread th[ M ];
for( int i = 0; i < M; ++i )
{
th[ i ] = std::thread( f, i, M, N );
}
for( int i = 0; i < M; ++i )
{
th[ i ].join();
}
BOOST_TEST_EQ( count, N * M );
return boost::report_errors();
}
+1 -1
View File
@@ -190,7 +190,7 @@ namespace {
map_type x2(vals2.size(), hasher(2), key_equal(1), allocator_type(3));
std::thread t1, t2, t3;
boost::latch l(2);
boost::compat::latch l(2);
std::mutex m;
std::condition_variable cv;
+1 -1
View File
@@ -520,7 +520,7 @@ namespace {
X x;
std::thread t1, t2;
boost::latch l(2);
boost::compat::latch l(2);
std::vector<std::string> strs(values.size());
t1 = std::thread([&l, &values, &x, &strs] {